1
1
package terraform_test
2
2
3
3
import (
4
+ "context"
4
5
"encoding/json"
5
6
"fmt"
6
7
"os"
@@ -14,11 +15,18 @@ import (
14
15
"github.com/stretchr/testify/require"
15
16
protobuf "google.golang.org/protobuf/proto"
16
17
18
+ "cdr.dev/slog"
19
+ "cdr.dev/slog/sloggers/slogtest"
20
+
17
21
"github.com/coder/coder/v2/cryptorand"
18
22
"github.com/coder/coder/v2/provisioner/terraform"
19
23
"github.com/coder/coder/v2/provisionersdk/proto"
20
24
)
21
25
26
+ func ctxAndLogger (t * testing.T ) (context.Context , slog.Logger ) {
27
+ return context .Background (), slogtest .Make (t , nil ).Leveled (slog .LevelDebug )
28
+ }
29
+
22
30
func TestConvertResources (t * testing.T ) {
23
31
t .Parallel ()
24
32
// nolint:dogsled
@@ -688,6 +696,7 @@ func TestConvertResources(t *testing.T) {
688
696
dir := filepath .Join (filepath .Dir (filename ), "testdata" , folderName )
689
697
t .Run ("Plan" , func (t * testing.T ) {
690
698
t .Parallel ()
699
+ ctx , logger := ctxAndLogger (t )
691
700
692
701
tfPlanRaw , err := os .ReadFile (filepath .Join (dir , folderName + ".tfplan.json" ))
693
702
require .NoError (t , err )
@@ -705,7 +714,7 @@ func TestConvertResources(t *testing.T) {
705
714
// and that no errors occur!
706
715
modules = append (modules , tfPlan .PlannedValues .RootModule )
707
716
}
708
- state , err := terraform .ConvertState (modules , string (tfPlanGraph ))
717
+ state , err := terraform .ConvertState (ctx , modules , string (tfPlanGraph ), logger )
709
718
require .NoError (t , err )
710
719
sortResources (state .Resources )
711
720
sortExternalAuthProviders (state .ExternalAuthProviders )
@@ -763,6 +772,7 @@ func TestConvertResources(t *testing.T) {
763
772
764
773
t .Run ("Provision" , func (t * testing.T ) {
765
774
t .Parallel ()
775
+ ctx , logger := ctxAndLogger (t )
766
776
tfStateRaw , err := os .ReadFile (filepath .Join (dir , folderName + ".tfstate.json" ))
767
777
require .NoError (t , err )
768
778
var tfState tfjson.State
@@ -771,7 +781,7 @@ func TestConvertResources(t *testing.T) {
771
781
tfStateGraph , err := os .ReadFile (filepath .Join (dir , folderName + ".tfstate.dot" ))
772
782
require .NoError (t , err )
773
783
774
- state , err := terraform .ConvertState ([]* tfjson.StateModule {tfState .Values .RootModule }, string (tfStateGraph ))
784
+ state , err := terraform .ConvertState (ctx , []* tfjson.StateModule {tfState .Values .RootModule }, string (tfStateGraph ), logger )
775
785
require .NoError (t , err )
776
786
sortResources (state .Resources )
777
787
sortExternalAuthProviders (state .ExternalAuthProviders )
@@ -808,6 +818,7 @@ func TestConvertResources(t *testing.T) {
808
818
809
819
func TestAppSlugValidation (t * testing.T ) {
810
820
t .Parallel ()
821
+ ctx , logger := ctxAndLogger (t )
811
822
812
823
// nolint:dogsled
813
824
_ , filename , _ , _ := runtime .Caller (0 )
@@ -829,7 +840,7 @@ func TestAppSlugValidation(t *testing.T) {
829
840
}
830
841
}
831
842
832
- state , err := terraform .ConvertState ([]* tfjson.StateModule {tfPlan .PlannedValues .RootModule }, string (tfPlanGraph ))
843
+ state , err := terraform .ConvertState (ctx , []* tfjson.StateModule {tfPlan .PlannedValues .RootModule }, string (tfPlanGraph ), logger )
833
844
require .Nil (t , state )
834
845
require .Error (t , err )
835
846
require .ErrorContains (t , err , "invalid app slug" )
@@ -841,14 +852,15 @@ func TestAppSlugValidation(t *testing.T) {
841
852
}
842
853
}
843
854
844
- state , err = terraform .ConvertState ([]* tfjson.StateModule {tfPlan .PlannedValues .RootModule }, string (tfPlanGraph ))
855
+ state , err = terraform .ConvertState (ctx , []* tfjson.StateModule {tfPlan .PlannedValues .RootModule }, string (tfPlanGraph ), logger )
845
856
require .Nil (t , state )
846
857
require .Error (t , err )
847
858
require .ErrorContains (t , err , "duplicate app slug" )
848
859
}
849
860
850
861
func TestMetadataResourceDuplicate (t * testing.T ) {
851
862
t .Parallel ()
863
+ ctx , logger := ctxAndLogger (t )
852
864
853
865
// Load the multiple-apps state file and edit it.
854
866
dir := filepath .Join ("testdata" , "resource-metadata-duplicate" )
@@ -860,14 +872,15 @@ func TestMetadataResourceDuplicate(t *testing.T) {
860
872
tfPlanGraph , err := os .ReadFile (filepath .Join (dir , "resource-metadata-duplicate.tfplan.dot" ))
861
873
require .NoError (t , err )
862
874
863
- state , err := terraform .ConvertState ([]* tfjson.StateModule {tfPlan .PlannedValues .RootModule }, string (tfPlanGraph ))
875
+ state , err := terraform .ConvertState (ctx , []* tfjson.StateModule {tfPlan .PlannedValues .RootModule }, string (tfPlanGraph ), logger )
864
876
require .Nil (t , state )
865
877
require .Error (t , err )
866
878
require .ErrorContains (t , err , "duplicate metadata resource: null_resource.about" )
867
879
}
868
880
869
881
func TestParameterValidation (t * testing.T ) {
870
882
t .Parallel ()
883
+ ctx , logger := ctxAndLogger (t )
871
884
872
885
// nolint:dogsled
873
886
_ , filename , _ , _ := runtime .Caller (0 )
@@ -891,7 +904,7 @@ func TestParameterValidation(t *testing.T) {
891
904
}
892
905
}
893
906
894
- state , err := terraform .ConvertState ([]* tfjson.StateModule {tfPlan .PriorState .Values .RootModule }, string (tfPlanGraph ))
907
+ state , err := terraform .ConvertState (ctx , []* tfjson.StateModule {tfPlan .PriorState .Values .RootModule }, string (tfPlanGraph ), logger )
895
908
require .Nil (t , state )
896
909
require .Error (t , err )
897
910
require .ErrorContains (t , err , "coder_parameter names must be unique but \" identical\" appears multiple times" )
@@ -907,7 +920,7 @@ func TestParameterValidation(t *testing.T) {
907
920
}
908
921
}
909
922
910
- state , err = terraform .ConvertState ([]* tfjson.StateModule {tfPlan .PriorState .Values .RootModule }, string (tfPlanGraph ))
923
+ state , err = terraform .ConvertState (ctx , []* tfjson.StateModule {tfPlan .PriorState .Values .RootModule }, string (tfPlanGraph ), logger )
911
924
require .Nil (t , state )
912
925
require .Error (t , err )
913
926
require .ErrorContains (t , err , "coder_parameter names must be unique but \" identical-0\" and \" identical-1\" appear multiple times" )
@@ -923,7 +936,7 @@ func TestParameterValidation(t *testing.T) {
923
936
}
924
937
}
925
938
926
- state , err = terraform .ConvertState ([]* tfjson.StateModule {tfPlan .PriorState .Values .RootModule }, string (tfPlanGraph ))
939
+ state , err = terraform .ConvertState (ctx , []* tfjson.StateModule {tfPlan .PriorState .Values .RootModule }, string (tfPlanGraph ), logger )
927
940
require .Nil (t , state )
928
941
require .Error (t , err )
929
942
require .ErrorContains (t , err , "coder_parameter names must be unique but \" identical-0\" , \" identical-1\" and \" identical-2\" appear multiple times" )
@@ -954,9 +967,10 @@ func TestInstanceTypeAssociation(t *testing.T) {
954
967
tc := tc
955
968
t .Run (tc .ResourceType , func (t * testing.T ) {
956
969
t .Parallel ()
970
+ ctx , logger := ctxAndLogger (t )
957
971
instanceType , err := cryptorand .String (12 )
958
972
require .NoError (t , err )
959
- state , err := terraform .ConvertState ([]* tfjson.StateModule {{
973
+ state , err := terraform .ConvertState (ctx , []* tfjson.StateModule {{
960
974
Resources : []* tfjson.StateResource {{
961
975
Address : tc .ResourceType + ".dev" ,
962
976
Type : tc .ResourceType ,
@@ -973,7 +987,7 @@ func TestInstanceTypeAssociation(t *testing.T) {
973
987
subgraph "root" {
974
988
"[root] ` + tc .ResourceType + `.dev" [label = "` + tc .ResourceType + `.dev", shape = "box"]
975
989
}
976
- }` )
990
+ }` , logger )
977
991
require .NoError (t , err )
978
992
require .Len (t , state .Resources , 1 )
979
993
require .Equal (t , state .Resources [0 ].GetInstanceType (), instanceType )
@@ -1012,9 +1026,10 @@ func TestInstanceIDAssociation(t *testing.T) {
1012
1026
tc := tc
1013
1027
t .Run (tc .ResourceType , func (t * testing.T ) {
1014
1028
t .Parallel ()
1029
+ ctx , logger := ctxAndLogger (t )
1015
1030
instanceID , err := cryptorand .String (12 )
1016
1031
require .NoError (t , err )
1017
- state , err := terraform .ConvertState ([]* tfjson.StateModule {{
1032
+ state , err := terraform .ConvertState (ctx , []* tfjson.StateModule {{
1018
1033
Resources : []* tfjson.StateResource {{
1019
1034
Address : "coder_agent.dev" ,
1020
1035
Type : "coder_agent" ,
@@ -1044,7 +1059,7 @@ func TestInstanceIDAssociation(t *testing.T) {
1044
1059
"[root] ` + tc .ResourceType + `.dev" -> "[root] coder_agent.dev"
1045
1060
}
1046
1061
}
1047
- ` )
1062
+ ` , logger )
1048
1063
require .NoError (t , err )
1049
1064
require .Len (t , state .Resources , 1 )
1050
1065
require .Len (t , state .Resources [0 ].Agents , 1 )
0 commit comments