11package examples_test
22
33import (
4- "sort"
54 "testing"
6- "time"
75
8- "github.com/outscale/osc-sdk-go/v3/pkg/options"
96 "github.com/outscale/osc-sdk-go/v3/pkg/osc"
10- "github.com/stretchr/testify/assert"
117 "github.com/stretchr/testify/require"
128)
139
@@ -26,149 +22,3 @@ func TestReadVms(t *testing.T) {
2622 t .Logf ("[%d] Id: %s; ImageId: %s" , i , vm .VmId , vm .ImageId )
2723 }
2824}
29-
30- // Steps done in this test:
31- // 1. Read an available subregion.
32- // 2. Read a public Ubuntu image.
33- // 3. Create a VM.
34- // 4. Add a tag to the VM.
35- // 5. Wait for the VM to be running.
36- // 6. Read the VM and validate its state and tag.
37- // 7. Delete the VM.
38- func TestVm (t * testing.T ) {
39- ctx := t .Context ()
40-
41- client := newOSCClient (t )
42-
43- subregions , err := client .ReadSubregions (ctx , osc.ReadSubregionsRequest {})
44- require .NoError (t , err )
45- require .NotNil (t , subregions .Subregions )
46- assert .NotEmpty (t , * subregions .Subregions )
47-
48- subregionName := (* subregions .Subregions )[0 ].SubregionName
49- require .NotNil (t , subregionName )
50- assert .NotEmpty (t , * subregionName )
51-
52- resultsPerPage := 10
53- publicImages := true
54- availableImages := []osc.ImageState {osc .ImageStateAvailable }
55- imageResp , err := client .ReadImages (ctx , osc.ReadImagesRequest {
56- Filters : & osc.FiltersImage {
57- AccountAliases : & []string {"Outscale" },
58- ImageNames : & []string {"Ubuntu*" },
59- PermissionsToLaunchGlobalPermission : & publicImages ,
60- States : & availableImages ,
61- },
62- ResultsPerPage : & resultsPerPage ,
63- })
64- require .NoError (t , err )
65- require .NotNil (t , imageResp .Images )
66- assert .NotEmpty (t , * imageResp .Images )
67-
68- images := append ([]osc.Image (nil ), (* imageResp .Images )... )
69- sort .Slice (images , func (i , j int ) bool {
70- return images [i ].CreationDate .Time .After (images [j ].CreationDate .Time )
71- })
72- imageID := images [0 ].ImageId
73- assert .NotEmpty (t , imageID )
74-
75- vmType := "tinav4.c1r1p2"
76- minVmsCount := 1
77- maxVmsCount := 1
78- tagKey := "Name"
79- tagValue := "osc-sdk-go-test-tag-" + RandomString (10 )
80-
81- createResp , err := client .CreateVms (ctx , osc.CreateVmsRequest {
82- ImageId : imageID ,
83- MinVmsCount : & minVmsCount ,
84- MaxVmsCount : & maxVmsCount ,
85- Placement : & osc.Placement {
86- SubregionName : * subregionName ,
87- Tenancy : "default" ,
88- },
89- VmType : & vmType ,
90- }, options .WithRetryTimeout (10 * time .Minute ))
91- require .NoError (t , err )
92-
93- deleted := false
94- defer func () {
95- if deleted {
96- return
97- }
98-
99- if createResp .Vms == nil || len (* createResp .Vms ) == 0 {
100- return
101- }
102-
103- _ , _ = client .DeleteVms (ctx , osc.DeleteVmsRequest {
104- VmIds : []string {(* createResp .Vms )[0 ].VmId },
105- }, options .WithRetryTimeout (10 * time .Minute ))
106- }()
107-
108- require .NotNil (t , createResp .Vms )
109- require .Len (t , * createResp .Vms , 1 )
110-
111- vmID := (* createResp .Vms )[0 ].VmId
112- assert .NotEmpty (t , vmID )
113-
114- _ , err = client .CreateTags (ctx , osc.CreateTagsJSONRequestBody {
115- ResourceIds : []string {vmID },
116- Tags : []osc.ResourceTag {
117- {
118- Key : tagKey ,
119- Value : tagValue ,
120- },
121- },
122- })
123- require .NoError (t , err )
124-
125- for range 36 {
126- readResp , err := client .ReadVms (ctx , osc.ReadVmsRequest {
127- Filters : & osc.FiltersVm {
128- VmIds : & []string {vmID },
129- },
130- })
131- require .NoError (t , err )
132- require .NotNil (t , readResp .Vms )
133- require .Len (t , * readResp .Vms , 1 )
134-
135- vm := (* readResp .Vms )[0 ]
136- t .Logf ("VM %s state: %s" , vmID , vm .State )
137- if vm .State == osc .VmStateRunning {
138- break
139- }
140- require .NotEqual (t , osc .VmStateTerminated , vm .State , "vm %s entered unexpected state %s" , vmID , vm .State )
141- require .NotEqual (t , osc .VmStateShuttingDown , vm .State , "vm %s entered unexpected state %s" , vmID , vm .State )
142-
143- time .Sleep (10 * time .Second )
144- }
145-
146- readResp , err := client .ReadVms (ctx , osc.ReadVmsRequest {
147- Filters : & osc.FiltersVm {
148- VmIds : & []string {vmID },
149- },
150- })
151- require .NoError (t , err )
152- require .NotNil (t , readResp .Vms )
153- require .Len (t , * readResp .Vms , 1 )
154-
155- vm := (* readResp .Vms )[0 ]
156- assert .Equal (t , osc .VmStateRunning , vm .State )
157- assert .Equal (t , imageID , vm .ImageId )
158- assert .Contains (t , vm .Tags , osc.ResourceTag {
159- Key : tagKey ,
160- Value : tagValue ,
161- }, "expected tag %q=%q on VM %s" , tagKey , tagValue , vmID )
162-
163- t .Logf ("Successfully read VM: %s" , vmID )
164-
165- deleteResp , err := client .DeleteVms (ctx , osc.DeleteVmsRequest {
166- VmIds : []string {vmID },
167- }, options .WithRetryTimeout (10 * time .Minute ))
168- require .NoError (t , err )
169- deleted = true
170- require .NotNil (t , deleteResp .ResponseContext )
171- assert .NotNil (t , deleteResp .ResponseContext .RequestId )
172-
173- t .Logf ("Successfully deleted VM: %s" , vmID )
174- }
0 commit comments