|
| 1 | +package wait |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "sync/atomic" |
| 6 | + "testing" |
| 7 | + "testing/synctest" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/google/go-cmp/cmp" |
| 11 | + "github.com/stackitcloud/stackit-sdk-go/core/oapierror" |
| 12 | + "github.com/stackitcloud/stackit-sdk-go/core/utils" |
| 13 | + runcommand "github.com/stackitcloud/stackit-sdk-go/services/runcommand/v1api" |
| 14 | +) |
| 15 | + |
| 16 | +type mockSettings struct { |
| 17 | + getFails bool |
| 18 | + resourceState runcommand.CommandDetailsStatus |
| 19 | +} |
| 20 | + |
| 21 | +func newAPIMock(settings mockSettings) runcommand.DefaultAPI { |
| 22 | + return &runcommand.DefaultAPIServiceMock{ |
| 23 | + GetCommandExecuteMock: utils.Ptr(func(_ runcommand.ApiGetCommandRequest) (*runcommand.CommandDetails, error) { |
| 24 | + if settings.getFails { |
| 25 | + return nil, &oapierror.GenericOpenAPIError{ |
| 26 | + StatusCode: 500, |
| 27 | + } |
| 28 | + } |
| 29 | + return &runcommand.CommandDetails{ |
| 30 | + Id: utils.Ptr(int32(1)), |
| 31 | + Status: utils.Ptr(settings.resourceState), |
| 32 | + }, nil |
| 33 | + }), |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +var testPayload = *runcommand.NewCreateCommandPayload("RunShellScript") |
| 38 | + |
| 39 | +func TestRunCommandWaitHandler(t *testing.T) { |
| 40 | + tests := []struct { |
| 41 | + desc string |
| 42 | + getFails bool |
| 43 | + resourceState runcommand.CommandDetailsStatus |
| 44 | + wantErr bool |
| 45 | + wantResp bool |
| 46 | + }{ |
| 47 | + { |
| 48 | + desc: "command completed", |
| 49 | + getFails: false, |
| 50 | + resourceState: runcommand.COMMANDDETAILSSTATUS_COMPLETED, |
| 51 | + wantErr: false, |
| 52 | + wantResp: true, |
| 53 | + }, |
| 54 | + { |
| 55 | + desc: "command failed", |
| 56 | + getFails: false, |
| 57 | + resourceState: runcommand.COMMANDDETAILSSTATUS_FAILED, |
| 58 | + wantErr: false, |
| 59 | + wantResp: true, |
| 60 | + }, |
| 61 | + { |
| 62 | + desc: "get fails", |
| 63 | + getFails: true, |
| 64 | + resourceState: runcommand.COMMANDDETAILSSTATUS_UNKNOWN_DEFAULT_OPEN_API, |
| 65 | + wantErr: true, |
| 66 | + wantResp: false, |
| 67 | + }, |
| 68 | + { |
| 69 | + desc: "timeout", |
| 70 | + getFails: false, |
| 71 | + resourceState: runcommand.COMMANDDETAILSSTATUS_RUNNING, |
| 72 | + wantErr: true, |
| 73 | + wantResp: false, |
| 74 | + }, |
| 75 | + } |
| 76 | + for _, tt := range tests { |
| 77 | + t.Run(tt.desc, func(t *testing.T) { |
| 78 | + synctest.Test(t, func(t *testing.T) { |
| 79 | + apiClient := newAPIMock(mockSettings{ |
| 80 | + getFails: tt.getFails, |
| 81 | + resourceState: tt.resourceState, |
| 82 | + }) |
| 83 | + |
| 84 | + var wantRes *runcommand.CommandDetails |
| 85 | + if tt.wantResp { |
| 86 | + wantRes = &runcommand.CommandDetails{ |
| 87 | + Id: utils.Ptr(int32(1)), |
| 88 | + Status: utils.Ptr(tt.resourceState), |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + handler := RunCommandWaitHandler(context.Background(), apiClient, "pid", "sid", "1") |
| 93 | + |
| 94 | + gotRes, err := handler.SetTimeout(10 * time.Millisecond).WaitWithContext(context.Background()) |
| 95 | + |
| 96 | + if (err != nil) != tt.wantErr { |
| 97 | + t.Fatalf("handler error = %v, wantErr %v", err, tt.wantErr) |
| 98 | + } |
| 99 | + if !cmp.Equal(gotRes, wantRes) { |
| 100 | + t.Fatalf("handler gotRes = %v, want %v", gotRes, wantRes) |
| 101 | + } |
| 102 | + }) |
| 103 | + }) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +func TestAgentReadyWaitHandler(t *testing.T) { |
| 108 | + tests := []struct { |
| 109 | + desc string |
| 110 | + createFn func(runcommand.ApiCreateCommandRequest) (*runcommand.NewCommandResponse, error) |
| 111 | + wantErr bool |
| 112 | + wantResp *runcommand.NewCommandResponse |
| 113 | + }{ |
| 114 | + { |
| 115 | + desc: "agent immediately ready", |
| 116 | + createFn: func(_ runcommand.ApiCreateCommandRequest) (*runcommand.NewCommandResponse, error) { |
| 117 | + return &runcommand.NewCommandResponse{Id: utils.Ptr(int32(42))}, nil |
| 118 | + }, |
| 119 | + wantErr: false, |
| 120 | + wantResp: &runcommand.NewCommandResponse{Id: utils.Ptr(int32(42))}, |
| 121 | + }, |
| 122 | + { |
| 123 | + desc: "agent not ready then ready", |
| 124 | + // atomic counter ensures the closure is safe when called from the handler goroutine |
| 125 | + createFn: func() func(runcommand.ApiCreateCommandRequest) (*runcommand.NewCommandResponse, error) { |
| 126 | + var calls atomic.Int32 |
| 127 | + return func(_ runcommand.ApiCreateCommandRequest) (*runcommand.NewCommandResponse, error) { |
| 128 | + if calls.Add(1) == 1 { |
| 129 | + return nil, &oapierror.GenericOpenAPIError{StatusCode: 404} |
| 130 | + } |
| 131 | + return &runcommand.NewCommandResponse{Id: utils.Ptr(int32(7))}, nil |
| 132 | + } |
| 133 | + }(), |
| 134 | + wantErr: false, |
| 135 | + wantResp: &runcommand.NewCommandResponse{Id: utils.Ptr(int32(7))}, |
| 136 | + }, |
| 137 | + { |
| 138 | + desc: "terminal error non 404", |
| 139 | + createFn: func(_ runcommand.ApiCreateCommandRequest) (*runcommand.NewCommandResponse, error) { |
| 140 | + return nil, &oapierror.GenericOpenAPIError{StatusCode: 500} |
| 141 | + }, |
| 142 | + wantErr: true, |
| 143 | + wantResp: nil, |
| 144 | + }, |
| 145 | + { |
| 146 | + desc: "timeout agent never ready", |
| 147 | + createFn: func(_ runcommand.ApiCreateCommandRequest) (*runcommand.NewCommandResponse, error) { |
| 148 | + return nil, &oapierror.GenericOpenAPIError{StatusCode: 404} |
| 149 | + }, |
| 150 | + wantErr: true, |
| 151 | + wantResp: nil, |
| 152 | + }, |
| 153 | + } |
| 154 | + |
| 155 | + for _, tt := range tests { |
| 156 | + t.Run(tt.desc, func(t *testing.T) { |
| 157 | + synctest.Test(t, func(t *testing.T) { |
| 158 | + apiClient := &runcommand.DefaultAPIServiceMock{ |
| 159 | + CreateCommandExecuteMock: utils.Ptr(tt.createFn), |
| 160 | + } |
| 161 | + |
| 162 | + handler := AgentReadyWaitHandler(context.Background(), apiClient, "pid", "sid", testPayload) |
| 163 | + |
| 164 | + // 1 ms throttle keeps the retry case within the 10 ms fake timeout |
| 165 | + gotRes, err := handler. |
| 166 | + SetThrottle(time.Millisecond). |
| 167 | + SetTimeout(10 * time.Millisecond). |
| 168 | + WaitWithContext(context.Background()) |
| 169 | + |
| 170 | + if (err != nil) != tt.wantErr { |
| 171 | + t.Fatalf("handler error = %v, wantErr %v", err, tt.wantErr) |
| 172 | + } |
| 173 | + if !cmp.Equal(gotRes, tt.wantResp) { |
| 174 | + t.Fatalf("handler gotRes = %v, want %v", gotRes, tt.wantResp) |
| 175 | + } |
| 176 | + }) |
| 177 | + }) |
| 178 | + } |
| 179 | +} |
0 commit comments