github.com/mmcquillan/packer@v1.1.1-0.20171009221028-c85cf0483a5d/builder/oracle/oci/client/instance_test.go (about) 1 package oci 2 3 import ( 4 "fmt" 5 "net/http" 6 "reflect" 7 "testing" 8 ) 9 10 func TestGetInstance(t *testing.T) { 11 setup() 12 defer teardown() 13 14 id := "ocid1.instance.oc1.phx.a" 15 path := fmt.Sprintf("/instances/%s", id) 16 mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) { 17 fmt.Fprintf(w, `{"id":"%s"}`, id) 18 }) 19 20 instance, err := client.Compute.Instances.Get(&GetInstanceParams{ID: id}) 21 if err != nil { 22 t.Errorf("Client.Compute.Instances.Get() returned error: %v", err) 23 } 24 25 want := Instance{ID: id} 26 27 if !reflect.DeepEqual(instance, want) { 28 t.Errorf("Client.Compute.Instances.Get() returned %+v, want %+v", instance, want) 29 } 30 } 31 32 func TestLaunchInstance(t *testing.T) { 33 setup() 34 defer teardown() 35 36 mux.HandleFunc("/instances/", func(w http.ResponseWriter, r *http.Request) { 37 fmt.Fprint(w, `{"displayName": "go-oci test"}`) 38 }) 39 40 params := &LaunchInstanceParams{ 41 AvailabilityDomain: "aaaa:PHX-AD-1", 42 CompartmentID: "ocid1.compartment.oc1..a", 43 DisplayName: "go-oci test", 44 ImageID: "ocid1.image.oc1.phx.a", 45 Shape: "VM.Standard1.1", 46 SubnetID: "ocid1.subnet.oc1.phx.a", 47 } 48 49 instance, err := client.Compute.Instances.Launch(params) 50 if err != nil { 51 t.Errorf("Client.Compute.Instances.Launch() returned error: %v", err) 52 } 53 54 want := Instance{DisplayName: "go-oci test"} 55 56 if !reflect.DeepEqual(instance, want) { 57 t.Errorf("Client.Compute.Instances.Launch() returned %+v, want %+v", instance, want) 58 } 59 } 60 61 func TestTerminateInstance(t *testing.T) { 62 setup() 63 defer teardown() 64 65 id := "ocid1.instance.oc1.phx.a" 66 path := fmt.Sprintf("/instances/%s", id) 67 mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) { 68 w.WriteHeader(http.StatusNoContent) 69 }) 70 71 err := client.Compute.Instances.Terminate(&TerminateInstanceParams{ID: id}) 72 if err != nil { 73 t.Errorf("Client.Compute.Instances.Terminate() returned error: %v", err) 74 } 75 } 76 77 func TestInstanceGetResourceState(t *testing.T) { 78 setup() 79 defer teardown() 80 81 id := "ocid1.instance.oc1.phx.a" 82 path := fmt.Sprintf("/instances/%s", id) 83 mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) { 84 fmt.Fprint(w, `{"LifecycleState": "RUNNING"}`) 85 }) 86 87 state, err := client.Compute.Instances.GetResourceState(id) 88 if err != nil { 89 t.Errorf("Client.Compute.Instances.GetResourceState() returned error: %v", err) 90 } 91 92 want := "RUNNING" 93 if state != want { 94 t.Errorf("Client.Compute.Instances.GetResourceState() returned %+v, want %+v", state, want) 95 } 96 }