github.com/sacloud/iaas-api-go@v1.12.0/helper/power/handlers_test.go (about) 1 // Copyright 2022-2023 The sacloud/iaas-api-go Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package power 16 17 import ( 18 "context" 19 "testing" 20 21 "github.com/sacloud/iaas-api-go" 22 "github.com/sacloud/iaas-api-go/types" 23 ) 24 25 func Test_serverHandler_boot(t *testing.T) { 26 type fields struct { 27 variables []string 28 } 29 tests := []struct { 30 name string 31 fields fields 32 wantErr bool 33 checkCalled func(*dummyServerAPI) bool 34 }{ 35 { 36 name: "no cloudConfig", 37 fields: fields{ 38 variables: nil, 39 }, 40 wantErr: false, 41 checkCalled: func(d *dummyServerAPI) bool { 42 return d.bootIsCalled 43 }, 44 }, 45 { 46 name: "cloudConfig is an empty string", 47 fields: fields{ 48 variables: []string{""}, 49 }, 50 wantErr: false, 51 checkCalled: func(d *dummyServerAPI) bool { 52 return d.bootIsCalled 53 }, 54 }, 55 { 56 name: "cloudConfig is a non-empty string", 57 fields: fields{ 58 variables: []string{"string"}, 59 }, 60 wantErr: false, 61 checkCalled: func(d *dummyServerAPI) bool { 62 return d.bootWithVariablesIsCalled 63 }, 64 }, 65 { 66 name: "cloudConfig is a slice of empty string", 67 fields: fields{ 68 variables: []string{"", ""}, 69 }, 70 wantErr: false, 71 checkCalled: func(d *dummyServerAPI) bool { 72 return d.bootIsCalled 73 }, 74 }, 75 { 76 name: "cloudConfig is a slice of string", 77 fields: fields{ 78 variables: []string{"string1", "string2"}, 79 }, 80 wantErr: false, 81 checkCalled: func(d *dummyServerAPI) bool { 82 return d.bootWithVariablesIsCalled 83 }, 84 }, 85 } 86 for _, tt := range tests { 87 t.Run(tt.name, func(t *testing.T) { 88 client := &dummyServerAPI{} 89 h := &serverHandler{ 90 ctx: context.Background(), 91 client: client, 92 zone: "is1a", 93 id: types.ID(1), 94 variables: tt.fields.variables, 95 } 96 if err := h.boot(); (err != nil) != tt.wantErr { 97 t.Errorf("boot() error = %v, wantErr %v", err, tt.wantErr) 98 } 99 if !tt.checkCalled(client) { 100 t.Errorf("unexpected function was called") 101 } 102 }) 103 } 104 } 105 106 type dummyServerAPI struct { 107 bootIsCalled bool 108 bootWithVariablesIsCalled bool 109 } 110 111 func (d *dummyServerAPI) Read(ctx context.Context, zone string, id types.ID) (*iaas.Server, error) { 112 return nil, nil 113 } 114 115 func (d *dummyServerAPI) Boot(ctx context.Context, zone string, id types.ID) error { 116 d.bootIsCalled = true 117 return nil 118 } 119 120 func (d *dummyServerAPI) BootWithVariables(ctx context.Context, zone string, id types.ID, param *iaas.ServerBootVariables) error { 121 d.bootWithVariablesIsCalled = true 122 return nil 123 } 124 125 func (d *dummyServerAPI) Shutdown(ctx context.Context, zone string, id types.ID, shutdownOption *iaas.ShutdownOption) error { 126 return nil 127 }