Skip to content

Commit b19958e

Browse files
committed
test(config): Add tests for loader.LoadConfigFile(...) to check that no configs are loaded for environments with Skip: true
1 parent 709c110 commit b19958e

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed

pkg/config/loader/config_loader_test.go

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/google/go-cmp/cmp"
2727
"github.com/spf13/afero"
2828
"github.com/stretchr/testify/assert"
29+
"github.com/stretchr/testify/require"
2930

3031
"github.com/dynatrace/dynatrace-configuration-as-code/v2/internal/featureflags"
3132
"github.com/dynatrace/dynatrace-configuration-as-code/v2/internal/pointer"
@@ -2108,6 +2109,108 @@ func Test_validateParameter(t *testing.T) {
21082109
}
21092110
}
21102111

2112+
// TestLoadConfigFile_ConfigsGeneratedForNonSkippedEnvironments tests that a config will be created for each non-skipped environment.
2113+
func TestLoadConfigFile_ConfigsGeneratedForNonSkippedEnvironments(t *testing.T) {
2114+
testLoaderContext := &LoaderContext{
2115+
ProjectId: "project",
2116+
Path: "some-dir/",
2117+
Environments: []manifest.EnvironmentDefinition{
2118+
{
2119+
Name: "environment1",
2120+
URL: manifest.URLDefinition{Type: manifest.ValueURLType, Value: "env url"},
2121+
Group: "group1",
2122+
Auth: manifest.Auth{
2123+
Token: &manifest.AuthSecret{Name: "token var"},
2124+
},
2125+
},
2126+
{
2127+
Name: "environment2",
2128+
URL: manifest.URLDefinition{Type: manifest.ValueURLType, Value: "env url"},
2129+
Group: "group1",
2130+
Auth: manifest.Auth{
2131+
Token: &manifest.AuthSecret{Name: "token var"},
2132+
},
2133+
},
2134+
},
2135+
ParametersSerDe: config.DefaultParameterParsers,
2136+
}
2137+
2138+
testFs := afero.NewMemMapFs()
2139+
require.NoError(t, afero.WriteFile(testFs, "config.yaml", []byte(`configs:
2140+
- id: profile-id
2141+
config:
2142+
name: 'Star Trek > Star Wars'
2143+
template: 'profile.json'
2144+
originObjectId: origin-object-id
2145+
type:
2146+
settings:
2147+
schema: 'builtin:profile.test'
2148+
schemaVersion: '1.0'
2149+
scope:
2150+
type: value
2151+
value: environment`), 0644))
2152+
require.NoError(t, afero.WriteFile(testFs, "profile.json", []byte("{}"), 0644))
2153+
2154+
configs, errs := LoadConfigFile(t.Context(), testFs, testLoaderContext, "config.yaml")
2155+
2156+
require.Empty(t, errs)
2157+
require.Len(t, configs, 2)
2158+
assert.Equal(t, "environment1", configs[0].Environment)
2159+
assert.Equal(t, "group1", configs[0].Group)
2160+
assert.Equal(t, "environment2", configs[1].Environment)
2161+
assert.Equal(t, "group1", configs[1].Group)
2162+
}
2163+
2164+
// TestLoadConfigFile_NoConfigsGeneratedForSkippedEnvironments tests that no config will be created for a skipped environment.
2165+
func TestLoadConfigFile_NoConfigsGeneratedForSkippedEnvironments(t *testing.T) {
2166+
testLoaderContext := &LoaderContext{
2167+
ProjectId: "project",
2168+
Path: "some-dir/",
2169+
Environments: []manifest.EnvironmentDefinition{
2170+
{
2171+
Name: "environment1",
2172+
URL: manifest.URLDefinition{Type: manifest.ValueURLType, Value: "env url"},
2173+
Group: "group1",
2174+
Auth: manifest.Auth{
2175+
Token: &manifest.AuthSecret{Name: "token var"},
2176+
},
2177+
},
2178+
{
2179+
Skip: true,
2180+
Name: "environment2",
2181+
URL: manifest.URLDefinition{Type: manifest.ValueURLType, Value: "env url"},
2182+
Group: "group1",
2183+
Auth: manifest.Auth{
2184+
Token: &manifest.AuthSecret{Name: "token var"},
2185+
},
2186+
},
2187+
},
2188+
ParametersSerDe: config.DefaultParameterParsers,
2189+
}
2190+
2191+
testFs := afero.NewMemMapFs()
2192+
require.NoError(t, afero.WriteFile(testFs, "config.yaml", []byte(`configs:
2193+
- id: profile-id
2194+
config:
2195+
name: 'Star Trek > Star Wars'
2196+
template: 'profile.json'
2197+
originObjectId: origin-object-id
2198+
type:
2199+
settings:
2200+
schema: 'builtin:profile.test'
2201+
schemaVersion: '1.0'
2202+
scope:
2203+
type: value
2204+
value: environment`), 0644))
2205+
require.NoError(t, afero.WriteFile(testFs, "profile.json", []byte("{}"), 0644))
2206+
2207+
configs, errs := LoadConfigFile(t.Context(), testFs, testLoaderContext, "config.yaml")
2208+
require.Empty(t, errs)
2209+
require.Len(t, configs, 1)
2210+
assert.Equal(t, "environment1", configs[0].Environment)
2211+
assert.Equal(t, "group1", configs[0].Group)
2212+
}
2213+
21112214
func makeCompoundParam(t *testing.T, refs []parameter.ParameterReference) *compound.CompoundParameter {
21122215
compoundParam, err := compound.New("param", "{}", refs)
21132216
assert.NoError(t, err)

0 commit comments

Comments
 (0)