Skip to content

Commit

Permalink
CI: fix CI for step_update_omi
Browse files Browse the repository at this point in the history
Signed-off-by: matthias.gatto <[email protected]>
  • Loading branch information
outscale-mgo committed Jun 13, 2024
1 parent 8a4a37e commit 5cd7a36
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 10 deletions.
7 changes: 5 additions & 2 deletions builder/common/step_update_omi.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ func (s *StepUpdateOMIAttributes) Run(_ context.Context, state multistep.StateBa
valid := false
valid = valid || (s.AccountIds != nil && len(s.AccountIds) > 0)
valid = valid || (s.SnapshotAccountIds != nil && len(s.SnapshotAccountIds) > 0)
valid = valid || (s.GlobalPermission != false)

valid = valid || s.GlobalPermission == true
if !valid {
return multistep.ActionContinue
}
Expand Down Expand Up @@ -75,6 +74,10 @@ func (s *StepUpdateOMIAttributes) Run(_ context.Context, state multistep.StateBa
}
}

if s.AccountIds == nil || len(s.AccountIds) == 0 {
return multistep.ActionContinue
}

// Updating snapshot attributes
for region, region_snapshots := range snapshots {
for _, snapshot := range region_snapshots {
Expand Down
73 changes: 65 additions & 8 deletions builder/common/step_update_omi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,90 @@ import (
"bytes"
"context"

"os"

packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
oscgo "github.com/outscale/osc-sdk-go/v2"
)

// Create statebag for running test
func tState() multistep.StateBag {
func tState(omiId string, snapId string) (multistep.StateBag, error) {
region := os.Getenv("OSC_REGION")
state := new(multistep.BasicStateBag)
accessConfig := &AccessConfig{}
accessConfig := &AccessConfig{
RawRegion: region,
}

oscConn, err := accessConfig.NewOSCClient()
if err != nil {
return nil, err
}
state.Put("osc", oscConn)
state.Put("ui", &packersdk.BasicUi{
Reader: new(bytes.Buffer),
Writer: new(bytes.Buffer),
})
state.Put("omis", map[string]string{"us-west-2": "omi-12345"})
state.Put("snapshots", map[string][]string{"us-west-2": {"snap-0012345"}})

omis:= make(map[string]string, 0)
omis[region] = omiId
state.Put("omis", omis)
snaps := make(map[string][]string, 0)
snaps[region] = []string{snapId}
state.Put("snapshots", snaps)
state.Put("accessConfig", accessConfig)
return state
return state, err
}

func TestUpdateOmi(t *testing.T) {

region := os.Getenv("OSC_REGION")
stepUpdateOMIAttributes := StepUpdateOMIAttributes{
AccountIds: []string{},
SnapshotAccountIds: []string{},
RawRegion: "us-west-2",
RawRegion: region,
GlobalPermission: true,
}
state := tState()
config := &AccessConfig{
RawRegion: region,
}

// Read vms to get 1 to copy
regionconn, _ := config.NewOSCClient()
readRet, _, err := regionconn.Api.VmApi.ReadVms(regionconn.Auth).ReadVmsRequest(oscgo.ReadVmsRequest{}).Execute()

Check failure on line 59 in builder/common/step_update_omi_test.go

View workflow job for this annotation

GitHub Actions / check-code

ineffectual assignment to err (ineffassign)
vm := readRet.GetVms()[0]
vmId := vm.GetVmId()
volId := vm.GetBlockDeviceMappings()[0].GetBsu().VolumeId

// Create new image
imgName := "ci-packer"
createRet, _, err := regionconn.Api.ImageApi.CreateImage(regionconn.Auth).

Check failure on line 66 in builder/common/step_update_omi_test.go

View workflow job for this annotation

GitHub Actions / check-code

ineffectual assignment to err (ineffassign)
CreateImageRequest(oscgo.CreateImageRequest{
ImageName: &imgName,
VmId: &vmId,
}).Execute()
imgId := createRet.Image.GetImageId()

// defer delete image
defer regionconn.Api.ImageApi.DeleteImage(regionconn.Auth).
DeleteImageRequest(oscgo.DeleteImageRequest{
ImageId: imgId,
}).Execute()

Check failure on line 77 in builder/common/step_update_omi_test.go

View workflow job for this annotation

GitHub Actions / check-code

Error return value of `(github.com/outscale/osc-sdk-go/v2.ApiDeleteImageRequest).Execute` is not checked (errcheck)

snapshotDesc := "snapshot-ci-packer"
createSnap, _, err := regionconn.Api.SnapshotApi.CreateSnapshot(regionconn.Auth).

Check failure on line 80 in builder/common/step_update_omi_test.go

View workflow job for this annotation

GitHub Actions / check-code

ineffectual assignment to err (ineffassign)
CreateSnapshotRequest(oscgo.CreateSnapshotRequest{
Description: &snapshotDesc,
VolumeId: volId,
}).Execute()
snapId := createSnap.Snapshot.GetSnapshotId()
defer regionconn.Api.SnapshotApi.DeleteSnapshot(regionconn.Auth).
DeleteSnapshotRequest(oscgo.DeleteSnapshotRequest{
SnapshotId: snapId,
}).Execute()

Check failure on line 89 in builder/common/step_update_omi_test.go

View workflow job for this annotation

GitHub Actions / check-code

Error return value of `(github.com/outscale/osc-sdk-go/v2.ApiDeleteSnapshotRequest).Execute` is not checked (errcheck)

state, err := tState(imgId, snapId)
if state == nil {
t.Fatalf("error retrieving state %s", err.Error())
}

action := stepUpdateOMIAttributes.Run(context.Background(), state)
if err := state.Get("error"); err != nil {
Expand Down

0 comments on commit 5cd7a36

Please sign in to comment.