@@ -34,6 +34,7 @@ func TestAnalyze(t *testing.T) {
3434 alerts alerter.Alerts
3535 expected Analysis
3636 hasDrifted bool
37+ options * AnalyzerOptions
3738 }{
3839 {
3940 name : "TestNilValues" , // Cover division by zero case
@@ -980,6 +981,162 @@ func TestAnalyze(t *testing.T) {
980981 },
981982 },
982983 },
984+ {
985+ name : "Test --only-managed flag" ,
986+ iac : []* resource.Resource {
987+ {
988+ Id : "foo" ,
989+ Type : aws .AwsInstanceResourceType ,
990+ Attrs : & resource.Attributes {
991+ "instance_type" : "test2" ,
992+ },
993+ },
994+ {
995+ Id : "baz" ,
996+ Type : aws .AwsInstanceResourceType ,
997+ Attrs : & resource.Attributes {
998+ "instance_type" : "test3" ,
999+ },
1000+ },
1001+ },
1002+ cloud : []* resource.Resource {
1003+ {
1004+ Id : "foo" ,
1005+ Type : aws .AwsInstanceResourceType ,
1006+ Attrs : & resource.Attributes {
1007+ "instance_type" : "test1" ,
1008+ },
1009+ },
1010+ {
1011+ Id : "bar" ,
1012+ Type : aws .AwsInstanceResourceType ,
1013+ Attrs : & resource.Attributes {
1014+ "instance_type" : "test2" ,
1015+ },
1016+ },
1017+ },
1018+ hasDrifted : true ,
1019+ expected : Analysis {
1020+ managed : []* resource.Resource {
1021+ {
1022+ Id : "foo" ,
1023+ Type : aws .AwsInstanceResourceType ,
1024+ Attrs : & resource.Attributes {
1025+ "instance_type" : "test2" ,
1026+ },
1027+ },
1028+ },
1029+ deleted : []* resource.Resource {
1030+ {
1031+ Id : "baz" ,
1032+ Type : aws .AwsInstanceResourceType ,
1033+ Attrs : & resource.Attributes {
1034+ "instance_type" : "test3" ,
1035+ },
1036+ },
1037+ },
1038+ differences : []Difference {
1039+ {
1040+ Res : & resource.Resource {
1041+ Id : "foo" ,
1042+ Type : aws .AwsInstanceResourceType ,
1043+ Attrs : & resource.Attributes {
1044+ "instance_type" : "test2" ,
1045+ },
1046+ },
1047+ Changelog : Changelog {
1048+ {
1049+ Change : diff.Change {
1050+ Type : "update" ,
1051+ From : "test2" ,
1052+ To : "test1" ,
1053+ Path : []string {
1054+ "instance_type" ,
1055+ },
1056+ },
1057+ },
1058+ },
1059+ },
1060+ },
1061+ summary : Summary {
1062+ TotalResources : 2 ,
1063+ TotalManaged : 1 ,
1064+ TotalUnmanaged : 0 ,
1065+ TotalDeleted : 1 ,
1066+ TotalDrifted : 1 ,
1067+ },
1068+ },
1069+ options : & AnalyzerOptions {
1070+ OnlyManaged : true ,
1071+ Deep : true ,
1072+ },
1073+ },
1074+ {
1075+ name : "Test --only-unmanaged flag" ,
1076+ iac : []* resource.Resource {
1077+ {
1078+ Id : "foo" ,
1079+ Type : aws .AwsInstanceResourceType ,
1080+ Attrs : & resource.Attributes {
1081+ "instance_type" : "test2" ,
1082+ },
1083+ },
1084+ {
1085+ Id : "baz" ,
1086+ Type : aws .AwsInstanceResourceType ,
1087+ Attrs : & resource.Attributes {
1088+ "instance_type" : "test3" ,
1089+ },
1090+ },
1091+ },
1092+ cloud : []* resource.Resource {
1093+ {
1094+ Id : "foo" ,
1095+ Type : aws .AwsInstanceResourceType ,
1096+ Attrs : & resource.Attributes {
1097+ "instance_type" : "test1" ,
1098+ },
1099+ },
1100+ {
1101+ Id : "bar" ,
1102+ Type : aws .AwsInstanceResourceType ,
1103+ Attrs : & resource.Attributes {
1104+ "instance_type" : "test2" ,
1105+ },
1106+ },
1107+ },
1108+ hasDrifted : true ,
1109+ expected : Analysis {
1110+ managed : []* resource.Resource {
1111+ {
1112+ Id : "foo" ,
1113+ Type : aws .AwsInstanceResourceType ,
1114+ Attrs : & resource.Attributes {
1115+ "instance_type" : "test2" ,
1116+ },
1117+ },
1118+ },
1119+ unmanaged : []* resource.Resource {
1120+ {
1121+ Id : "bar" ,
1122+ Type : aws .AwsInstanceResourceType ,
1123+ Attrs : & resource.Attributes {
1124+ "instance_type" : "test2" ,
1125+ },
1126+ },
1127+ },
1128+ summary : Summary {
1129+ TotalResources : 2 ,
1130+ TotalManaged : 1 ,
1131+ TotalUnmanaged : 1 ,
1132+ TotalDeleted : 0 ,
1133+ TotalDrifted : 0 ,
1134+ },
1135+ },
1136+ options : & AnalyzerOptions {
1137+ OnlyUnmanaged : true ,
1138+ },
1139+ },
9831140 }
9841141
9851142 differ , err := diff .NewDiffer (diff .SliceOrdering (true ))
@@ -1008,7 +1165,12 @@ func TestAnalyze(t *testing.T) {
10081165 repo := testresource .InitFakeSchemaRepository ("aws" , "3.19.0" )
10091166 aws .InitResourcesMetadata (repo )
10101167
1011- analyzer := NewAnalyzer (al , AnalyzerOptions {Deep : true }, testFilter )
1168+ options := AnalyzerOptions {Deep : true }
1169+ if c .options != nil {
1170+ options = * c .options
1171+ }
1172+
1173+ analyzer := NewAnalyzer (al , options , testFilter )
10121174
10131175 for _ , res := range c .cloud {
10141176 addSchemaToRes (res , repo )
0 commit comments