15
15
package convert
16
16
17
17
import (
18
- "encoding/json"
19
18
"testing"
20
19
21
20
"github.com/stretchr/testify/assert"
@@ -24,8 +23,9 @@ import (
24
23
"github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1"
25
24
)
26
25
27
- func Test_V1Alpha1to2_Config (t * testing.T ) {
28
- config := `---
26
+ func Test_V1Alpha1to2 (t * testing.T ) {
27
+ t .Run ("valid config" , func (t * testing.T ) {
28
+ config := `---
29
29
receivers:
30
30
otlp:
31
31
protocols:
@@ -43,19 +43,29 @@ service:
43
43
processors: [resourcedetection]
44
44
exporters: [otlp]
45
45
`
46
- cfgV1 := v1alpha1.OpenTelemetryCollector {
47
- Spec : v1alpha1.OpenTelemetryCollectorSpec {
48
- Config : config ,
49
- },
50
- }
51
-
52
- cfgV2 , err := V1Alpha1to2 (cfgV1 )
53
- assert .Nil (t , err )
54
- assert .NotNil (t , cfgV2 )
55
-
56
- jsonCfg , err := json .Marshal (& cfgV2 .Spec .Config )
57
- assert .Nil (t , err )
58
- yamlCfg , err := yaml .JSONToYAML (jsonCfg )
59
- assert .Nil (t , err )
60
- assert .YAMLEq (t , config , string (yamlCfg ))
46
+ cfgV1 := v1alpha1.OpenTelemetryCollector {
47
+ Spec : v1alpha1.OpenTelemetryCollectorSpec {
48
+ Config : config ,
49
+ },
50
+ }
51
+
52
+ cfgV2 , err := V1Alpha1to2 (cfgV1 )
53
+ assert .Nil (t , err )
54
+ assert .NotNil (t , cfgV2 )
55
+
56
+ yamlCfg , err := yaml .Marshal (& cfgV2 .Spec .Config )
57
+ assert .Nil (t , err )
58
+ assert .YAMLEq (t , config , string (yamlCfg ))
59
+ })
60
+ t .Run ("invalid config" , func (t * testing.T ) {
61
+ config := `!!!`
62
+ cfgV1 := v1alpha1.OpenTelemetryCollector {
63
+ Spec : v1alpha1.OpenTelemetryCollectorSpec {
64
+ Config : config ,
65
+ },
66
+ }
67
+
68
+ _ , err := V1Alpha1to2 (cfgV1 )
69
+ assert .ErrorContains (t , err , "could not convert config json to v1alpha2.Config" )
70
+ })
61
71
}
0 commit comments