1717package project
1818
1919import (
20+ "bytes"
2021 "fmt"
2122 "io/fs"
2223 "reflect"
@@ -27,6 +28,7 @@ import (
2728 "github.com/stretchr/testify/require"
2829
2930 "github.com/dynatrace/dynatrace-configuration-as-code/v2/internal/errutils"
31+ "github.com/dynatrace/dynatrace-configuration-as-code/v2/internal/log"
3032 "github.com/dynatrace/dynatrace-configuration-as-code/v2/internal/testutils"
3133 "github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/config"
3234 "github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/config/coordinate"
@@ -1481,6 +1483,112 @@ func TestLoadProjects_NetworkZonesContainsParameterToSetting(t *testing.T) {
14811483 assert .Contains (t , networkZone2 .Parameters , "__MONACO_NZONE_ENABLED__" )
14821484}
14831485
1486+ // TestLoadProjects_EnvironmentOverrideWithUndefinedEnvironmentProducesWarning tests that referencing an undefined environment in an environment override produces a warning.
1487+ func TestLoadProjects_EnvironmentOverrideWithUndefinedEnvironmentProducesWarning (t * testing.T ) {
1488+ managementZoneConfig := []byte (`configs:
1489+ - id: mz
1490+ config:
1491+ template: mz.json
1492+ type:
1493+ settings:
1494+ schema: builtin:management-zones
1495+ scope: environment
1496+ environmentOverrides:
1497+ - environment: prod
1498+ override:
1499+ skip: true
1500+ ` )
1501+
1502+ managementZoneJSON := []byte (`{ "name": "", "rules": [] }` )
1503+
1504+ testFs := testutils .TempFs (t )
1505+ logSpy := bytes.Buffer {}
1506+ log .PrepareLogging (t .Context (), afero .NewMemMapFs (), false , & logSpy , false , false )
1507+
1508+ require .NoError (t , testFs .MkdirAll ("a/builtinmanagement-zones" , testDirectoryFileMode ))
1509+ require .NoError (t , afero .WriteFile (testFs , "a/builtinmanagement-zones/config.yaml" , managementZoneConfig , testFileFileMode ))
1510+ require .NoError (t , afero .WriteFile (testFs , "a/builtinmanagement-zones/mz.json" , managementZoneJSON , testFileFileMode ))
1511+
1512+ testContext := ProjectLoaderContext {
1513+ KnownApis : map [string ]struct {}{"builtin:management-zones" : {}},
1514+ WorkingDir : "." ,
1515+ Manifest : manifest.Manifest {
1516+ Projects : manifest.ProjectDefinitionByProjectID {
1517+ "a" : {
1518+ Name : "a" ,
1519+ Path : "a/" ,
1520+ },
1521+ },
1522+ Environments : manifest.Environments {
1523+ "dev" : {
1524+ Name : "dev" ,
1525+ Auth : manifest.Auth {Token : & manifest.AuthSecret {Name : "ENV_VAR" }},
1526+ },
1527+ },
1528+ },
1529+ ParametersSerde : config .DefaultParameterParsers ,
1530+ }
1531+
1532+ gotProjects , gotErrs := LoadProjects (t .Context (), testFs , testContext , nil )
1533+ assert .Len (t , gotErrs , 0 , "Expected no errors loading dependent projects " )
1534+ assert .Len (t , gotProjects , 1 )
1535+
1536+ assert .Contains (t , logSpy .String (), "unknown environment" )
1537+ }
1538+
1539+ // TestLoadProjects_GroupOverrideWithUndefinedGroupProducesWarning tests that referencing an undefined environment group in a group override produces a warning.
1540+ func TestLoadProjects_GroupOverrideWithUndefinedGroupProducesWarning (t * testing.T ) {
1541+ managementZoneConfig := []byte (`configs:
1542+ - id: mz
1543+ config:
1544+ template: mz.json
1545+ type:
1546+ settings:
1547+ schema: builtin:management-zones
1548+ scope: environment
1549+ groupOverrides:
1550+ - group: prod
1551+ override:
1552+ skip: true
1553+ ` )
1554+
1555+ managementZoneJSON := []byte (`{ "name": "", "rules": [] }` )
1556+
1557+ testFs := testutils .TempFs (t )
1558+
1559+ logSpy := bytes.Buffer {}
1560+ log .PrepareLogging (t .Context (), afero .NewMemMapFs (), false , & logSpy , false , false )
1561+
1562+ require .NoError (t , testFs .MkdirAll ("a/builtinmanagement-zones" , testDirectoryFileMode ))
1563+ require .NoError (t , afero .WriteFile (testFs , "a/builtinmanagement-zones/config.yaml" , managementZoneConfig , testFileFileMode ))
1564+ require .NoError (t , afero .WriteFile (testFs , "a/builtinmanagement-zones/mz.json" , managementZoneJSON , testFileFileMode ))
1565+ testContext := ProjectLoaderContext {
1566+ KnownApis : map [string ]struct {}{"builtin:management-zones" : {}},
1567+ WorkingDir : "." ,
1568+ Manifest : manifest.Manifest {
1569+ Projects : manifest.ProjectDefinitionByProjectID {
1570+ "a" : {
1571+ Name : "a" ,
1572+ Path : "a/" ,
1573+ },
1574+ },
1575+ Environments : manifest.Environments {
1576+ "dev" : {
1577+ Name : "dev" ,
1578+ Auth : manifest.Auth {Token : & manifest.AuthSecret {Name : "ENV_VAR" }},
1579+ },
1580+ },
1581+ },
1582+ ParametersSerde : config .DefaultParameterParsers ,
1583+ }
1584+
1585+ gotProjects , gotErrs := LoadProjects (t .Context (), testFs , testContext , nil )
1586+ assert .Len (t , gotErrs , 0 , "Expected no errors loading dependent projects " )
1587+ assert .Len (t , gotProjects , 1 )
1588+
1589+ assert .Contains (t , logSpy .String (), "unknown group" )
1590+ }
1591+
14841592type propResolver func (coordinate.Coordinate , string ) (any , bool )
14851593
14861594func (p propResolver ) GetResolvedProperty (coordinate coordinate.Coordinate , propertyName string ) (any , bool ) {
0 commit comments