@@ -33,65 +33,143 @@ func Test_CLI_Hooks_RegistrationAndRun(t *testing.T) {
3333 require .NotEmpty (t , subscriptionId , "hooks smoke test requires a subscription id" )
3434
3535 location := cfg .Location
36- if session != nil && session .Playback {
37- location = "eastus2"
38- }
3936 require .NotEmpty (t , location , "hooks smoke test requires a location" )
4037
41- predeployTracePath := filepath .Join (dir , "predeploy -trace.log" )
38+ deployTracePath := filepath .Join (dir , "deploy -trace.log" )
4239 provisionTracePath := filepath .Join (dir , "provision-trace.log" )
4340
4441 cli := azdcli .NewCLI (t , azdcli .WithSession (session ))
4542 cli .WorkingDirectory = dir
46- baseEnv := append (os .Environ (),
47- fmt .Sprintf ("AZURE_SUBSCRIPTION_ID=%s" , subscriptionId ),
48- fmt .Sprintf ("AZURE_LOCATION=%s" , location ),
49- "AZD_ALPHA_ENABLE_LLM=false" ,
50- )
51- setHookTraceEnv := func (tracePath string ) {
52- cli .Env = append ([]string {}, baseEnv ... )
53- cli .Env = append (cli .Env , fmt .Sprintf ("HOOK_TRACE_FILE=%s" , tracePath ))
54- }
43+ baseEnv := hooksTestEnv (subscriptionId , location )
44+ cli .Env = append (os .Environ (), baseEnv ... )
45+
46+ err := copySample (dir , "hooks" )
47+ require .NoError (t , err , "failed expanding sample" )
48+
49+ _ , err = cli .RunCommandWithStdIn (ctx , stdinForInit (envName ), "init" )
50+ require .NoError (t , err )
51+
52+ setHookTraceEnv (cli , baseEnv , deployTracePath )
53+ _ , err = cli .RunCommand (ctx , "deploy" )
54+ require .Error (t , err , "deploy should fail for this hooks sample" )
55+
56+ setHookTraceEnv (cli , baseEnv , provisionTracePath )
57+ _ , err = cli .RunCommand (ctx , "provision" )
58+ require .Error (t , err , "provision should fail for this hooks sample" )
59+
60+ require .Equal (t , []string {
61+ "command-predeploy" ,
62+ "service-prerestore" ,
63+ }, readTraceEntries (t , deployTracePath ))
64+
65+ require .Equal (t , []string {
66+ "command-preprovision" ,
67+ "layer-preprovision" ,
68+ }, readTraceEntries (t , provisionTracePath ))
69+ }
70+
71+ func Test_CLI_Hooks_Run_RegistrationAndRun (t * testing.T ) {
72+ t .Parallel ()
5573
56- readTraceEntries := func (tracePath string ) [] string {
57- traceBytes , err := os . ReadFile ( tracePath )
74+ t . Run ( "RunAll" , func (t * testing. T ) {
75+ traceEntries , err := runLocalHooksCommand ( t , "predeploy" )
5876 require .NoError (t , err )
5977
60- var traceEntries []string
61- for _ , line := range strings .Split (string (traceBytes ), "\n " ) {
62- line = strings .TrimSpace (line )
63- if line == "" {
64- continue
65- }
78+ require .Equal (t , []string {
79+ "command-predeploy" ,
80+ "service-predeploy" ,
81+ }, traceEntries )
82+ })
83+
84+ t .Run ("RunSpecific" , func (t * testing.T ) {
85+ t .Run ("Service" , func (t * testing.T ) {
86+ traceEntries , err := runLocalHooksCommand (t , "predeploy" , "--service" , "app" )
87+ require .NoError (t , err )
88+
89+ require .Equal (t , []string {
90+ "command-predeploy" ,
91+ "service-predeploy" ,
92+ }, traceEntries )
93+ })
94+
95+ t .Run ("Layer" , func (t * testing.T ) {
96+ traceEntries , err := runLocalHooksCommand (t , "preprovision" , "--layer" , "core" )
97+ require .Error (t , err )
98+
99+ require .Equal (t , []string {
100+ "command-preprovision" ,
101+ "layer-preprovision" ,
102+ }, traceEntries )
103+ })
104+ })
105+ }
66106
67- traceEntries = append (traceEntries , line )
68- }
107+ func hooksTestEnv (subscriptionId string , location string ) []string {
108+ baseEnv := append (os .Environ (), "AZD_ALPHA_ENABLE_LLM=false" )
109+ if subscriptionId != "" {
110+ baseEnv = append (baseEnv , fmt .Sprintf ("AZURE_SUBSCRIPTION_ID=%s" , subscriptionId ))
111+ }
69112
70- return traceEntries
113+ if location != "" {
114+ baseEnv = append (baseEnv , fmt .Sprintf ("AZURE_LOCATION=%s" , location ))
71115 }
72116
117+ return baseEnv
118+ }
119+
120+ func setHookTraceEnv (cli * azdcli.CLI , baseEnv []string , tracePath string ) {
121+ cli .Env = append ([]string {}, baseEnv ... )
122+ cli .Env = append (cli .Env , fmt .Sprintf ("HOOK_TRACE_FILE=%s" , tracePath ))
123+ }
124+
125+ func runLocalHooksCommand (t * testing.T , args ... string ) ([]string , error ) {
126+ t .Helper ()
127+
128+ ctx , cancel := newTestContext (t )
129+ defer cancel ()
130+
131+ dir := tempDirWithDiagnostics (t )
132+ t .Logf ("DIR: %s" , dir )
133+
134+ envName := randomOrStoredEnvName (nil )
135+ t .Logf ("AZURE_ENV_NAME: %s" , envName )
136+
137+ cli := azdcli .NewCLI (t )
138+ cli .WorkingDirectory = dir
139+
140+ baseEnv := hooksTestEnv ("" , "" )
141+ tracePath := filepath .Join (dir , "hooks-run-trace.log" )
142+
73143 err := copySample (dir , "hooks" )
74144 require .NoError (t , err , "failed expanding sample" )
75145
76146 cli .Env = append ([]string {}, baseEnv ... )
77147 _ , err = cli .RunCommandWithStdIn (ctx , stdinForInit (envName ), "init" )
78148 require .NoError (t , err )
79149
80- setHookTraceEnv (predeployTracePath )
81- _ , err = cli .RunCommand (ctx , "hooks" , "run" , "predeploy" , "--service" , "app" )
150+ setHookTraceEnv (cli , baseEnv , tracePath )
151+
152+ command := append ([]string {"hooks" , "run" }, args ... )
153+ _ , err = cli .RunCommand (ctx , command ... )
154+
155+ return readTraceEntries (t , tracePath ), err
156+ }
157+
158+ func readTraceEntries (t * testing.T , tracePath string ) []string {
159+ t .Helper ()
160+
161+ traceBytes , err := os .ReadFile (tracePath )
82162 require .NoError (t , err )
83163
84- setHookTraceEnv (provisionTracePath )
85- _ , err = cli .RunCommand (ctx , "provision" )
86- require .Error (t , err , "provision should fail for this hooks sample" )
164+ var traceEntries []string
165+ for _ , line := range strings .Split (string (traceBytes ), "\n " ) {
166+ line = strings .TrimSpace (line )
167+ if line == "" {
168+ continue
169+ }
87170
88- require .Equal (t , []string {
89- "command-predeploy" ,
90- "service-predeploy" ,
91- }, readTraceEntries (predeployTracePath ))
171+ traceEntries = append (traceEntries , line )
172+ }
92173
93- require .Equal (t , []string {
94- "command-preprovision" ,
95- "layer-preprovision" ,
96- }, readTraceEntries (provisionTracePath ))
174+ return traceEntries
97175}
0 commit comments