2016-03-17 04:34:29 +00:00
package godo
import (
"encoding/json"
"fmt"
"net/http"
"reflect"
"testing"
)
var (
2016-12-21 00:00:17 +00:00
listEmptyJSON = `
2016-03-17 04:34:29 +00:00
{
"tags" : [
] ,
"meta" : {
"total" : 0
}
}
`
2016-12-21 00:00:17 +00:00
listJSON = `
2016-03-17 04:34:29 +00:00
{
"tags" : [
{
"name" : "testing-1" ,
"resources" : {
2018-09-04 14:08:59 +00:00
"count" : 0 ,
2016-03-17 04:34:29 +00:00
"droplets" : {
"count" : 0 ,
"last_tagged" : null
2018-09-04 14:08:59 +00:00
} ,
"images" : {
"count" : 0
2016-03-17 04:34:29 +00:00
}
}
} ,
{
"name" : "testing-2" ,
"resources" : {
2018-09-04 14:08:59 +00:00
"count" : 0 ,
2016-03-17 04:34:29 +00:00
"droplets" : {
"count" : 0 ,
"last_tagged" : null
2018-09-04 14:08:59 +00:00
} ,
"images" : {
"count" : 0
2016-03-17 04:34:29 +00:00
}
}
}
] ,
"links" : {
2017-04-07 14:53:34 +00:00
"pages" : {
2016-03-17 04:34:29 +00:00
"next" : "http://example.com/v2/tags/?page=3" ,
"prev" : "http://example.com/v2/tags/?page=1" ,
"last" : "http://example.com/v2/tags/?page=3" ,
"first" : "http://example.com/v2/tags/?page=1"
}
} ,
"meta" : {
"total" : 2
}
}
`
2016-12-21 00:00:17 +00:00
createJSON = `
2016-03-17 04:34:29 +00:00
{
"tag" : {
"name" : "testing-1" ,
"resources" : {
2018-09-04 14:08:59 +00:00
"count" : 0 ,
2016-03-17 04:34:29 +00:00
"droplets" : {
"count" : 0 ,
"last_tagged" : null
2018-09-04 14:08:59 +00:00
} ,
"images" : {
"count" : 0
2016-03-17 04:34:29 +00:00
}
}
}
}
`
2016-12-21 00:00:17 +00:00
getJSON = `
2016-03-17 04:34:29 +00:00
{
"tag" : {
"name" : "testing-1" ,
"resources" : {
2018-09-04 14:08:59 +00:00
"count" : 2 ,
"last_tagged_uri" : "https://api.digitalocean.com/v2/droplets/1" ,
2016-03-17 04:34:29 +00:00
"droplets" : {
"count" : 1 ,
2018-09-04 14:08:59 +00:00
"last_tagged_uri" : "https://api.digitalocean.com/v2/droplets/1" ,
2016-03-17 04:34:29 +00:00
"last_tagged" : {
"id" : 1 ,
"name" : "test.example.com" ,
"memory" : 1024 ,
"vcpus" : 2 ,
"disk" : 20 ,
"region" : {
"slug" : "nyc1" ,
"name" : "New York" ,
"sizes" : [
"1024mb" ,
"512mb"
] ,
"available" : true ,
"features" : [
"virtio" ,
"private_networking" ,
"backups" ,
"ipv6"
]
} ,
"image" : {
"id" : 119192817 ,
"name" : "Ubuntu 13.04" ,
"distribution" : "ubuntu" ,
"slug" : "ubuntu1304" ,
"public" : true ,
"regions" : [
"nyc1"
] ,
"created_at" : "2014-07-29T14:35:37Z"
} ,
"size_slug" : "1024mb" ,
"locked" : false ,
"status" : "active" ,
"networks" : {
"v4" : [
{
"ip_address" : "10.0.0.19" ,
"netmask" : "255.255.0.0" ,
"gateway" : "10.0.0.1" ,
"type" : "private"
} ,
{
"ip_address" : "127.0.0.19" ,
"netmask" : "255.255.255.0" ,
"gateway" : "127.0.0.20" ,
"type" : "public"
}
] ,
"v6" : [
{
"ip_address" : "2001::13" ,
"cidr" : 124 ,
"gateway" : "2400:6180:0000:00D0:0000:0000:0009:7000" ,
"type" : "public"
}
]
} ,
"kernel" : {
"id" : 485432985 ,
"name" : "DO-recovery-static-fsck" ,
"version" : "3.8.0-25-generic"
} ,
"created_at" : "2014-07-29T14:35:37Z" ,
"features" : [
"ipv6"
] ,
"backup_ids" : [
449676382
] ,
"snapshot_ids" : [
449676383
] ,
"action_ids" : [
] ,
"tags" : [
"tag-1" ,
"tag-2"
]
}
2018-09-04 14:08:59 +00:00
} ,
"images" : {
"count" : 1 ,
"last_tagged_uri" : "https://api.digitalocean.com/v2/images/1"
2016-03-17 04:34:29 +00:00
}
}
}
2016-11-29 02:42:23 +00:00
}
2016-03-17 04:34:29 +00:00
`
)
func TestTags_List ( t * testing . T ) {
setup ( )
defer teardown ( )
mux . HandleFunc ( "/v2/tags" , func ( w http . ResponseWriter , r * http . Request ) {
2017-07-02 11:55:56 +00:00
testMethod ( t , r , http . MethodGet )
2016-12-21 00:00:17 +00:00
fmt . Fprint ( w , listJSON )
2016-03-17 04:34:29 +00:00
} )
2016-11-29 02:42:23 +00:00
tags , _ , err := client . Tags . List ( ctx , nil )
2016-03-17 04:34:29 +00:00
if err != nil {
2016-05-12 16:34:11 +00:00
t . Errorf ( "Tags.List returned error: %v" , err )
2016-03-17 04:34:29 +00:00
}
2018-09-04 14:08:59 +00:00
expected := [ ] Tag { { Name : "testing-1" , Resources : & TaggedResources { Count : 0 , Droplets : & TaggedDropletsResources { Count : 0 , LastTagged : nil } , Images : & TaggedImagesResources { Count : 0 } } } ,
{ Name : "testing-2" , Resources : & TaggedResources { Count : 0 , Droplets : & TaggedDropletsResources { Count : 0 , LastTagged : nil } , Images : & TaggedImagesResources { Count : 0 } } } }
2016-03-17 04:34:29 +00:00
if ! reflect . DeepEqual ( tags , expected ) {
t . Errorf ( "Tags.List returned %+v, expected %+v" , tags , expected )
}
}
func TestTags_ListEmpty ( t * testing . T ) {
setup ( )
defer teardown ( )
mux . HandleFunc ( "/v2/tags" , func ( w http . ResponseWriter , r * http . Request ) {
2017-07-02 11:55:56 +00:00
testMethod ( t , r , http . MethodGet )
2016-12-21 00:00:17 +00:00
fmt . Fprint ( w , listEmptyJSON )
2016-03-17 04:34:29 +00:00
} )
2016-11-29 02:42:23 +00:00
tags , _ , err := client . Tags . List ( ctx , nil )
2016-03-17 04:34:29 +00:00
if err != nil {
2016-05-12 16:34:11 +00:00
t . Errorf ( "Tags.List returned error: %v" , err )
2016-03-17 04:34:29 +00:00
}
expected := [ ] Tag { }
if ! reflect . DeepEqual ( tags , expected ) {
t . Errorf ( "Tags.List returned %+v, expected %+v" , tags , expected )
}
}
func TestTags_ListPaging ( t * testing . T ) {
setup ( )
defer teardown ( )
mux . HandleFunc ( "/v2/tags" , func ( w http . ResponseWriter , r * http . Request ) {
2017-07-02 11:55:56 +00:00
testMethod ( t , r , http . MethodGet )
2016-12-21 00:00:17 +00:00
fmt . Fprint ( w , listJSON )
2016-03-17 04:34:29 +00:00
} )
2016-11-29 02:42:23 +00:00
_ , resp , err := client . Tags . List ( ctx , nil )
2016-03-17 04:34:29 +00:00
if err != nil {
2016-05-12 16:34:11 +00:00
t . Errorf ( "Tags.List returned error: %v" , err )
2016-03-17 04:34:29 +00:00
}
checkCurrentPage ( t , resp , 2 )
}
func TestTags_Get ( t * testing . T ) {
setup ( )
defer teardown ( )
mux . HandleFunc ( "/v2/tags/testing-1" , func ( w http . ResponseWriter , r * http . Request ) {
2017-07-02 11:55:56 +00:00
testMethod ( t , r , http . MethodGet )
2016-12-21 00:00:17 +00:00
fmt . Fprint ( w , getJSON )
2016-03-17 04:34:29 +00:00
} )
2016-11-29 02:42:23 +00:00
tag , _ , err := client . Tags . Get ( ctx , "testing-1" )
2016-03-17 04:34:29 +00:00
if err != nil {
2016-05-12 16:34:11 +00:00
t . Errorf ( "Tags.Get returned error: %v" , err )
2016-03-17 04:34:29 +00:00
}
if tag . Name != "testing-1" {
2016-05-12 16:34:11 +00:00
t . Errorf ( "Tags.Get return an incorrect name, got %+v, expected %+v" , tag . Name , "testing-1" )
2016-03-17 04:34:29 +00:00
}
2018-09-04 14:08:59 +00:00
if tag . Resources . Count != 2 {
t . Errorf ( "Tags.Get return an incorrect resource count, got %+v, expected %+v" , tag . Resources . Count , 2 )
}
if tag . Resources . LastTaggedURI != "https://api.digitalocean.com/v2/droplets/1" {
t . Errorf ( "Tags.Get return an incorrect last tagged uri %+v, expected %+v" , tag . Resources . LastTaggedURI , "https://api.digitalocean.com/v2/droplets/1" )
}
2016-03-17 04:34:29 +00:00
if tag . Resources . Droplets . Count != 1 {
2016-05-12 16:34:11 +00:00
t . Errorf ( "Tags.Get return an incorrect droplet resource count, got %+v, expected %+v" , tag . Resources . Droplets . Count , 1 )
2016-03-17 04:34:29 +00:00
}
if tag . Resources . Droplets . LastTagged . ID != 1 {
2016-05-12 16:34:11 +00:00
t . Errorf ( "Tags.Get return an incorrect last tagged droplet %+v, expected %+v" , tag . Resources . Droplets . LastTagged . ID , 1 )
2016-03-17 04:34:29 +00:00
}
2018-09-04 14:08:59 +00:00
if tag . Resources . Droplets . LastTaggedURI != "https://api.digitalocean.com/v2/droplets/1" {
t . Errorf ( "Tags.Get return an incorrect last tagged droplet uri %+v, expected %+v" , tag . Resources . Droplets . LastTaggedURI , "https://api.digitalocean.com/v2/droplets/1" )
}
if tag . Resources . Images . Count != 1 {
t . Errorf ( "Tags.Get return an incorrect image resource count, got %+v, expected %+v" , tag . Resources . Images . Count , 1 )
}
if tag . Resources . Images . LastTaggedURI != "https://api.digitalocean.com/v2/images/1" {
t . Errorf ( "Tags.Get return an incorrect last tagged droplet uri %+v, expected %+v" , tag . Resources . Images . LastTaggedURI , "https://api.digitalocean.com/v2/images/1" )
}
2016-03-17 04:34:29 +00:00
}
func TestTags_Create ( t * testing . T ) {
setup ( )
defer teardown ( )
createRequest := & TagCreateRequest {
Name : "testing-1" ,
}
mux . HandleFunc ( "/v2/tags" , func ( w http . ResponseWriter , r * http . Request ) {
v := new ( TagCreateRequest )
err := json . NewDecoder ( r . Body ) . Decode ( v )
if err != nil {
t . Fatalf ( "decode json: %v" , err )
}
2017-07-02 11:55:56 +00:00
testMethod ( t , r , http . MethodPost )
2016-03-17 04:34:29 +00:00
if ! reflect . DeepEqual ( v , createRequest ) {
t . Errorf ( "Request body = %+v, expected %+v" , v , createRequest )
}
2016-12-21 00:00:17 +00:00
fmt . Fprintf ( w , createJSON )
2016-03-17 04:34:29 +00:00
} )
2016-11-29 02:42:23 +00:00
tag , _ , err := client . Tags . Create ( ctx , createRequest )
2016-03-17 04:34:29 +00:00
if err != nil {
t . Errorf ( "Tags.Create returned error: %v" , err )
}
2018-09-04 14:08:59 +00:00
expected := & Tag { Name : "testing-1" , Resources : & TaggedResources { Count : 0 , Droplets : & TaggedDropletsResources { Count : 0 , LastTagged : nil } , Images : & TaggedImagesResources { Count : 0 } } }
2016-03-17 04:34:29 +00:00
if ! reflect . DeepEqual ( tag , expected ) {
t . Errorf ( "Tags.Create returned %+v, expected %+v" , tag , expected )
}
}
func TestTags_Delete ( t * testing . T ) {
setup ( )
defer teardown ( )
mux . HandleFunc ( "/v2/tags/testing-1" , func ( w http . ResponseWriter , r * http . Request ) {
2017-07-02 11:55:56 +00:00
testMethod ( t , r , http . MethodDelete )
2016-03-17 04:34:29 +00:00
} )
2016-11-29 02:42:23 +00:00
_ , err := client . Tags . Delete ( ctx , "testing-1" )
2016-03-17 04:34:29 +00:00
if err != nil {
t . Errorf ( "Tags.Delete returned error: %v" , err )
}
}
func TestTags_TagResource ( t * testing . T ) {
setup ( )
defer teardown ( )
tagResourcesRequest := & TagResourcesRequest {
2018-09-04 14:08:59 +00:00
Resources : [ ] Resource {
{ ID : "1" , Type : DropletResourceType } ,
{ ID : "1" , Type : ImageResourceType } ,
} ,
2016-03-17 04:34:29 +00:00
}
mux . HandleFunc ( "/v2/tags/testing-1/resources" , func ( w http . ResponseWriter , r * http . Request ) {
v := new ( TagResourcesRequest )
err := json . NewDecoder ( r . Body ) . Decode ( v )
if err != nil {
t . Fatalf ( "decode json: %v" , err )
}
2017-07-02 11:55:56 +00:00
testMethod ( t , r , http . MethodPost )
2016-03-17 04:34:29 +00:00
if ! reflect . DeepEqual ( v , tagResourcesRequest ) {
t . Errorf ( "Request body = %+v, expected %+v" , v , tagResourcesRequest )
}
} )
2016-11-29 02:42:23 +00:00
_ , err := client . Tags . TagResources ( ctx , "testing-1" , tagResourcesRequest )
2016-03-17 04:34:29 +00:00
if err != nil {
t . Errorf ( "Tags.TagResources returned error: %v" , err )
}
}
func TestTags_UntagResource ( t * testing . T ) {
setup ( )
defer teardown ( )
untagResourcesRequest := & UntagResourcesRequest {
2018-09-04 14:08:59 +00:00
Resources : [ ] Resource {
{ ID : "1" , Type : DropletResourceType } ,
{ ID : "1" , Type : ImageResourceType } ,
} ,
2016-03-17 04:34:29 +00:00
}
mux . HandleFunc ( "/v2/tags/testing-1/resources" , func ( w http . ResponseWriter , r * http . Request ) {
v := new ( UntagResourcesRequest )
err := json . NewDecoder ( r . Body ) . Decode ( v )
if err != nil {
t . Fatalf ( "decode json: %v" , err )
}
2017-07-02 11:55:56 +00:00
testMethod ( t , r , http . MethodDelete )
2016-03-17 04:34:29 +00:00
if ! reflect . DeepEqual ( v , untagResourcesRequest ) {
t . Errorf ( "Request body = %+v, expected %+v" , v , untagResourcesRequest )
}
} )
2016-11-29 02:42:23 +00:00
_ , err := client . Tags . UntagResources ( ctx , "testing-1" , untagResourcesRequest )
2016-03-17 04:34:29 +00:00
if err != nil {
t . Errorf ( "Tags.UntagResources returned error: %v" , err )
}
}