|
| 1 | +package otelcomponentmapping_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "os" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/stackvista/stackstate-cli/cmd/otelcomponentmapping" |
| 9 | + "github.com/stackvista/stackstate-cli/generated/stackstate_api" |
| 10 | + "github.com/stackvista/stackstate-cli/internal/di" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | + "gopkg.in/yaml.v2" |
| 14 | +) |
| 15 | + |
| 16 | +func createTempYamlFile(t *testing.T, content string) string { |
| 17 | + t.Helper() |
| 18 | + tmpfile, err := os.CreateTemp(os.TempDir(), "test_componentmapping_*.yaml") |
| 19 | + if err != nil { |
| 20 | + panic(err) |
| 21 | + } |
| 22 | + |
| 23 | + _, err = tmpfile.Write([]byte(content)) |
| 24 | + assert.NoError(t, err) |
| 25 | + tmpfile.Close() |
| 26 | + return tmpfile.Name() |
| 27 | +} |
| 28 | + |
| 29 | +func toOtelMappingItem(m stackstate_api.OtelComponentMapping) stackstate_api.OtelMappingItem { |
| 30 | + return stackstate_api.OtelMappingItem{ |
| 31 | + Name: m.Name, |
| 32 | + Identifier: &m.Identifier, |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +func TestOtelComponentMappingApply_SuccessYaml(t *testing.T) { |
| 37 | + cli := di.NewMockDeps(t) |
| 38 | + cmd := otelcomponentmapping.OtelComponentMappingApplyCommand(&cli.Deps) |
| 39 | + mapping := stackstate_api.OtelComponentMapping{ |
| 40 | + Name: "my-mapping", |
| 41 | + Identifier: "urn:otel:component:foo", |
| 42 | + } |
| 43 | + yamlContent, err := yaml.Marshal(mapping) |
| 44 | + assert.NoError(t, err) |
| 45 | + tmpFile := createTempYamlFile(t, string(yamlContent)) |
| 46 | + defer os.Remove(tmpFile) |
| 47 | + |
| 48 | + want := toOtelMappingItem(mapping) |
| 49 | + cli.MockClient.ApiMocks.OtelMappingApi.UpsertOtelComponentMappingsResponse.Result = want |
| 50 | + |
| 51 | + di.ExecuteCommandWithContextUnsafe(&cli.Deps, cmd, "-f", tmpFile) |
| 52 | + |
| 53 | + assert.Len(t, *cli.MockClient.ApiMocks.OtelMappingApi.UpsertOtelComponentMappingsCalls, 1) |
| 54 | + upsertCall := (*cli.MockClient.ApiMocks.OtelMappingApi.UpsertOtelComponentMappingsCalls)[0] |
| 55 | + assert.Equal(t, mapping.Identifier, upsertCall.PupsertOtelComponentMappingsRequest.Identifier) |
| 56 | + assert.Equal(t, mapping.Name, upsertCall.PupsertOtelComponentMappingsRequest.Name) |
| 57 | +} |
| 58 | + |
| 59 | +func TestOtelComponentMappingApply_SuccessJson(t *testing.T) { |
| 60 | + cli := di.NewMockDeps(t) |
| 61 | + cmd := otelcomponentmapping.OtelComponentMappingApplyCommand(&cli.Deps) |
| 62 | + mapping := stackstate_api.OtelComponentMapping{ |
| 63 | + Name: "my-mapping", |
| 64 | + Identifier: "urn:otel:component:foo", |
| 65 | + } |
| 66 | + yamlContent, err := yaml.Marshal(mapping) |
| 67 | + assert.NoError(t, err) |
| 68 | + tmpFile := createTempYamlFile(t, string(yamlContent)) |
| 69 | + defer os.Remove(tmpFile) |
| 70 | + |
| 71 | + want := toOtelMappingItem(mapping) |
| 72 | + cli.MockClient.ApiMocks.OtelMappingApi.UpsertOtelComponentMappingsResponse.Result = want |
| 73 | + |
| 74 | + di.ExecuteCommandWithContextUnsafe(&cli.Deps, cmd, "-f", tmpFile, "-o", "json") |
| 75 | + |
| 76 | + assert.Len(t, *cli.MockClient.ApiMocks.OtelMappingApi.UpsertOtelComponentMappingsCalls, 1) |
| 77 | + |
| 78 | + output := *cli.MockPrinter.PrintJsonCalls |
| 79 | + assert.Len(t, output, 1) |
| 80 | + val, ok := output[0]["otel_component_mapping"] |
| 81 | + assert.True(t, ok) |
| 82 | + actualPtr, ok := val.(*stackstate_api.OtelMappingItem) |
| 83 | + if ok { |
| 84 | + assert.Equal(t, &want, actualPtr) |
| 85 | + } else { |
| 86 | + actualVal, ok := val.(stackstate_api.OtelMappingItem) |
| 87 | + require.True(t, ok, "PrintJsonCalls type is not OtelMappingItem or *OtelMappingItem") |
| 88 | + assert.Equal(t, want, actualVal) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +func TestOtelComponentMappingApply_FileNotFound(t *testing.T) { |
| 93 | + cli := di.NewMockDeps(t) |
| 94 | + cmd := otelcomponentmapping.OtelComponentMappingApplyCommand(&cli.Deps) |
| 95 | + badPath := "/not/a/real/path.yaml" |
| 96 | + require.Panics(t, func() { |
| 97 | + di.ExecuteCommandWithContextUnsafe(&cli.Deps, cmd, "-f", badPath) |
| 98 | + }, "expected panic for missing file during apply command execution") |
| 99 | +} |
| 100 | + |
| 101 | +func TestOtelComponentMappingApply_WrongExtension(t *testing.T) { |
| 102 | + cli := di.NewMockDeps(t) |
| 103 | + cmd := otelcomponentmapping.OtelComponentMappingApplyCommand(&cli.Deps) |
| 104 | + tmpFile := createTempYamlFile(t, "irrelevant-content") |
| 105 | + newPath := tmpFile + ".txt" |
| 106 | + err := os.Rename(tmpFile, newPath) |
| 107 | + assert.NoError(t, err) |
| 108 | + defer os.Remove(newPath) |
| 109 | + require.Panics(t, func() { |
| 110 | + di.ExecuteCommandWithContextUnsafe(&cli.Deps, cmd, "-f", newPath) |
| 111 | + }, "expected panic for wrong extension during apply command execution") |
| 112 | +} |
| 113 | + |
| 114 | +func TestOtelComponentMappingApply_InvalidYaml(t *testing.T) { |
| 115 | + cli := di.NewMockDeps(t) |
| 116 | + cmd := otelcomponentmapping.OtelComponentMappingApplyCommand(&cli.Deps) |
| 117 | + tmpFile := createTempYamlFile(t, "not: [valid, yaml,,,") |
| 118 | + defer os.Remove(tmpFile) |
| 119 | + require.Panics(t, func() { |
| 120 | + di.ExecuteCommandWithContextUnsafe(&cli.Deps, cmd, "-f", tmpFile) |
| 121 | + }, "expected panic for invalid YAML during apply command execution") |
| 122 | +} |
| 123 | + |
| 124 | +func TestOtelComponentMappingApply_ApiError(t *testing.T) { |
| 125 | + cli := di.NewMockDeps(t) |
| 126 | + cmd := otelcomponentmapping.OtelComponentMappingApplyCommand(&cli.Deps) |
| 127 | + mapping := stackstate_api.OtelComponentMapping{ |
| 128 | + Name: "my-mapping", |
| 129 | + Identifier: "urn:otel:component:foo", |
| 130 | + } |
| 131 | + yamlContent, err := yaml.Marshal(mapping) |
| 132 | + assert.NoError(t, err) |
| 133 | + tmpFile := createTempYamlFile(t, string(yamlContent)) |
| 134 | + defer os.Remove(tmpFile) |
| 135 | + |
| 136 | + cli.MockClient.ApiMocks.OtelMappingApi.UpsertOtelComponentMappingsResponse.Error = errors.New("api failure") |
| 137 | + |
| 138 | + require.Panics(t, func() { |
| 139 | + di.ExecuteCommandWithContextUnsafe(&cli.Deps, cmd, "-f", tmpFile) |
| 140 | + }, "expected panic for API error during apply command execution") |
| 141 | +} |
0 commit comments