File: tag_test.go

package info (click to toggle)
golang-github-linode-linodego 1.55.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,112 kB
  • sloc: makefile: 96; sh: 52; python: 24
file content (119 lines) | stat: -rw-r--r-- 3,380 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package unit

import (
	"context"
	"fmt"
	"testing"

	"github.com/linode/linodego"
	"github.com/stretchr/testify/assert"
	"golang.org/x/exp/slices"
)

func TestListTags(t *testing.T) {
	// Load the fixture data for tags
	fixtureData, err := fixtures.GetFixture("tags_list")
	assert.NoError(t, err)

	var base ClientBaseCase
	base.SetUp(t)
	defer base.TearDown(t)

	base.MockGet("tags", fixtureData)

	tags, err := base.Client.ListTags(context.Background(), &linodego.ListOptions{})
	assert.NoError(t, err)

	assert.NotEmpty(t, tags, "Expected non-empty tag list")

	// Check if a specific tag exists using slices.ContainsFunc
	exists := slices.ContainsFunc(tags, func(tag linodego.Tag) bool {
		return tag.Label == "example-tag"
	})

	assert.True(t, exists, "Expected tag list to contain 'example-tag'")
}

func TestCreateTag(t *testing.T) {
	// Load the fixture data for tag creation
	fixtureData, err := fixtures.GetFixture("tag_create")
	assert.NoError(t, err)

	var base ClientBaseCase
	base.SetUp(t)
	defer base.TearDown(t)

	base.MockPost("tags", fixtureData)

	opts := linodego.TagCreateOptions{
		Label: "new-tag",
	}

	tag, err := base.Client.CreateTag(context.Background(), opts)
	assert.NoError(t, err, "Expected no error when creating tag")

	// Verify the created tag's label
	assert.Equal(t, "new-tag", tag.Label, "Expected created tag label to match input")
}

func TestDeleteTag(t *testing.T) {
	var base ClientBaseCase
	base.SetUp(t)
	defer base.TearDown(t)

	tagLabel := "delete-tag"
	base.MockDelete(fmt.Sprintf("tags/%s", tagLabel), nil)

	err := base.Client.DeleteTag(context.Background(), tagLabel)
	assert.NoError(t, err, "Expected no error when deleting tag")
}

func TestListTaggedObjects(t *testing.T) {
	// Load the fixture data for tagged objects
	fixtureData, err := fixtures.GetFixture("tagged_objects_list")
	assert.NoError(t, err)

	var base ClientBaseCase
	base.SetUp(t)
	defer base.TearDown(t)

	tagLabel := "example-tag"
	base.MockGet(fmt.Sprintf("tags/%s", tagLabel), fixtureData)

	taggedObjects, err := base.Client.ListTaggedObjects(context.Background(), tagLabel, &linodego.ListOptions{})
	assert.NoError(t, err)

	assert.NotEmpty(t, taggedObjects, "Expected non-empty tagged objects list")

	// Find the specific tagged object using slices.IndexFunc
	index := slices.IndexFunc(taggedObjects, func(obj linodego.TaggedObject) bool {
		return obj.Type == "linode"
	})

	assert.NotEqual(t, -1, index, "Expected to find a tagged object of type 'linode'")
	if index != -1 {
		assert.Equal(t, "linode", taggedObjects[index].Type, "Expected tagged object type to be 'linode'")
	}
}

func TestSortedObjects(t *testing.T) {
	// Load the fixture data for tagged objects
	fixtureData, err := fixtures.GetFixture("tagged_objects_list")
	assert.NoError(t, err)

	var base ClientBaseCase
	base.SetUp(t)
	defer base.TearDown(t)

	tagLabel := "example-tag"
	base.MockGet(fmt.Sprintf("tags/%s", tagLabel), fixtureData)

	taggedObjects, err := base.Client.ListTaggedObjects(context.Background(), tagLabel, &linodego.ListOptions{})
	assert.NoError(t, err)

	sortedObjects, err := taggedObjects.SortedObjects()
	assert.NoError(t, err)

	assert.NotEmpty(t, sortedObjects.Instances, "Expected non-empty instances list in sorted objects")
	assert.Equal(t, "example-instance", sortedObjects.Instances[0].Label, "Expected instance label to be 'example-instance'")
}