From 5cd7a364294df4c79aa824f747901ad80f65e919 Mon Sep 17 00:00:00 2001 From: "matthias.gatto" Date: Tue, 11 Jun 2024 15:58:47 +0200 Subject: [PATCH] CI: fix CI for step_update_omi Signed-off-by: matthias.gatto --- builder/common/step_update_omi.go | 7 ++- builder/common/step_update_omi_test.go | 73 +++++++++++++++++++++++--- 2 files changed, 70 insertions(+), 10 deletions(-) diff --git a/builder/common/step_update_omi.go b/builder/common/step_update_omi.go index 905b277d..e3d186c3 100644 --- a/builder/common/step_update_omi.go +++ b/builder/common/step_update_omi.go @@ -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 } @@ -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 { diff --git a/builder/common/step_update_omi_test.go b/builder/common/step_update_omi_test.go index 1bfda48c..46f028ce 100644 --- a/builder/common/step_update_omi_test.go +++ b/builder/common/step_update_omi_test.go @@ -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() + 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). + 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() + + snapshotDesc := "snapshot-ci-packer" + createSnap, _, err := regionconn.Api.SnapshotApi.CreateSnapshot(regionconn.Auth). + 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() + + 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 {