func TestClient(t *testing.T) { c := sl.NewSoftlayerWithOptions(opts) f := &sl.Filter{ Datacenter: "sjc01", } d := time.Now() templates, err := c.TemplatesByFilter(f) if err = nonil(err, validate(templates)); err != nil { t.Fatal(err) } reqDur := time.Now().Sub(d) // BUG(Softlayer): Even though the following should return single // result, it has "Softlayer-Total-Items: 2" and single item in // the payload. The item, however, has empty Datacenters field which // causes the test to fail. Does objectFilter zeroes it? d = time.Now() xtemplates, err := c.XTemplatesByFilter(f) if err = nonil(err, validate(xtemplates)); err != nil { t.Logf("BUG(Softlayer): %s", err) } xreqDur := time.Now().Sub(d) t.Logf("[TEST] filtering took: client-side=%s, server-side=%s", reqDur, xreqDur) if len(templates) != len(xtemplates) { t.Logf("BUG(Softlayer): want len(templates)=%d == len(xtemplates)=%d\n", len(templates), len(xtemplates)) } }
func TestKeys(t *testing.T) { pem := os.Getenv("KLOUD_USER_PRIVATEKEY") if pem == "" { t.Skip("skipping, KLOUD_USER_PRIVATEKEY is empty") } c := sl.NewSoftlayerWithOptions(opts) key, err := sl.ParseKey(pem) if err != nil { t.Fatalf("NewKey(%q)=%s", pem, err) } key.Label = fmt.Sprintf("test-%s-%d", key.Label, time.Now().UnixNano()) newKey, err := c.CreateKey(key) if err != nil { t.Fatalf("CreateKey(%+v)=%s", key, err) } defer func() { if err := c.DeleteKey(newKey.ID); err != nil { t.Error(err) } }() if newKey.ID == 0 { t.Error("want key.ID != 0") } if newKey.Fingerprint != key.Fingerprint { t.Errorf("want fingerprint=%q; got %q", key.Fingerprint, newKey.Fingerprint) } if newKey.CreateDate.IsZero() { t.Errorf("want %v to be actual date", newKey.CreateDate) } f := &sl.Filter{ Label: key.Label, } d := time.Now() keys, err := c.KeysByFilter(f) if err != nil { t.Fatal(err) } reqDur := time.Now().Sub(d) d = time.Now() xkeys, err := c.XKeysByFilter(f) if err != nil { t.Fatal(err) } xreqDur := time.Now().Sub(d) t.Logf("[TEST] filtering took: client-side=%s, server-side=%s", reqDur, xreqDur) if len(keys) != 1 { t.Errorf("want len(keys)=1; got %d", len(keys)) } if len(xkeys) != 1 { t.Errorf("want len(xkeys)=1; got %d", len(keys)) } if !reflect.DeepEqual(keys[0], newKey) { t.Errorf("want key=%+v; got %+v", newKey, keys[0]) } if !reflect.DeepEqual(xkeys[0], newKey) { t.Errorf("want key=%+v; got %+v", newKey, xkeys[0]) } }
func TestInstance(t *testing.T) { c := sl.NewSoftlayerWithOptions(opts) f := &sl.Filter{} d := time.Now() instances, err := c.InstancesByFilter(f) if sl.IsNotFound(err) { t.Skip("dev environment has no instances created to test the API all") } if err != nil { t.Fatalf("InstancesByFilter(%v)=%s", f, err) } reqDur := time.Now().Sub(d) d = time.Now() xinstances, err := c.XInstancesByFilter(f) if sl.IsNotFound(err) { t.Skip("no instances found to perform filering / tag setting test") } if err != nil { t.Fatalf("XInstancesByFilter(%v)=%s", f, err) } xreqDur := time.Now().Sub(d) t.Logf("[TEST] filtering took: client-side=%s, server-side=%s", reqDur, xreqDur) if !reflect.DeepEqual(instances, xinstances) { t.Errorf("%+v != %+v", instances, xinstances) } id := instances[0].ID tags := instances[0].Tags.Copy() tags["test-tag"] = "test-value" err = c.InstanceSetTags(id, tags) if err != nil { t.Fatal(err) } f = &sl.Filter{ ID: id, Tags: map[string]string{ "tag-key": "", }, } instances, err = c.InstancesByFilter(f) if err != nil { t.Fatal(err) } tags = instances[0].Tags.Copy() if v := tags["test-tag"]; v != "test-value" { t.Errorf(`want v="test-value"; got %q`, v) } delete(tags, "test-tag") if err = c.InstanceSetTags(id, tags); err != nil { t.Fatal(err) } }
func TestLookupImage(t *testing.T) { c := sl.NewSoftlayerWithOptions(opts) f := &sl.Filter{ Tags: sl.Tags{ "Name": "koding-stable", }, } images, err := c.TemplatesByFilter(f) if err != nil { t.Fatal(err) } w := &tabwriter.Writer{} w.Init(os.Stdout, 0, 8, 0, '\t', 0) fmt.Fprintln(w, "ID\tGlobalID\tTags\tDatacenter\tDatacenters") for _, image := range images { fmt.Fprintf(w, "%d\t%s\t%s\t%v\t%v\n", image.ID, image.GlobalID, image.Tags, image.Datacenter, image.Datacenters) } w.Flush() }
func TestDatacenter(t *testing.T) { c := sl.NewSoftlayerWithOptions(opts) f := &sl.Filter{ Name: "sjc01", } d := time.Now() datacenters, err := c.DatacentersByFilter(f) if err != nil { t.Fatalf("DatacentersByFilter(%v)=%s", f, err) } reqDur := time.Now().Sub(d) d = time.Now() xdatacenters, err := c.XDatacentersByFilter(f) if err != nil { t.Fatalf("XDatacentersByFilter(%v)=%s", f, err) } xreqDur := time.Now().Sub(d) t.Logf("[TEST] filtering took: client-side=%s, server-side=%s", reqDur, xreqDur) if !reflect.DeepEqual(datacenters, xdatacenters) { t.Fatalf("%+v != %+v", datacenters, xdatacenters) } }