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