File: rs_test.go

package info (click to toggle)
golang-github-ncw-swift-v2 2.0.1-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 456 kB
  • sloc: sh: 61; python: 30; makefile: 4
file content (97 lines) | stat: -rw-r--r-- 2,155 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
// See swift_test.go for requirements to run this test.
package rs_test

import (
	"context"
	"os"
	"testing"

	"github.com/ncw/swift/v2/rs"
)

var (
	c rs.RsConnection
)

const (
	CONTAINER    = "GoSwiftUnitTest"
	OBJECT       = "test_object"
	CONTENTS     = "12345"
	CONTENT_SIZE = int64(len(CONTENTS))
	CONTENT_MD5  = "827ccb0eea8a706c4c34a16891f84e7b"
)

// Test functions are run in order - this one must be first!
func TestAuthenticate(t *testing.T) {
	UserName := os.Getenv("SWIFT_API_USER")
	ApiKey := os.Getenv("SWIFT_API_KEY")
	AuthUrl := os.Getenv("SWIFT_AUTH_URL")
	if UserName == "" || ApiKey == "" || AuthUrl == "" {
		t.Fatal("SWIFT_API_USER, SWIFT_API_KEY and SWIFT_AUTH_URL not all set")
	}
	c = rs.RsConnection{}
	c.UserName = UserName
	c.ApiKey = ApiKey
	c.AuthUrl = AuthUrl
	err := c.Authenticate(context.Background())
	if err != nil {
		t.Fatal("Auth failed", err)
	}
	if !c.Authenticated() {
		t.Fatal("Not authenticated")
	}
}

// Setup
func TestContainerCreate(t *testing.T) {
	err := c.ContainerCreate(context.Background(), CONTAINER, nil)
	if err != nil {
		t.Fatal(err)
	}
}

func TestCDNEnable(t *testing.T) {
	headers, err := c.ContainerCDNEnable(context.Background(), CONTAINER, 0)
	if err != nil {
		t.Error(err)
	}
	if _, ok := headers["X-Cdn-Uri"]; !ok {
		t.Error("Failed to enable CDN for container")
	}
}

func TestOnReAuth(t *testing.T) {
	c2 := rs.RsConnection{}
	c2.UserName = c.UserName
	c2.ApiKey = c.ApiKey
	c2.AuthUrl = c.AuthUrl
	_, err := c2.ContainerCDNEnable(context.Background(), CONTAINER, 0)
	if err != nil {
		t.Fatalf("Failed to reauthenticate: %v", err)
	}
}

func TestCDNMeta(t *testing.T) {
	headers, err := c.ContainerCDNMeta(context.Background(), CONTAINER)
	if err != nil {
		t.Error(err)
	}
	if _, ok := headers["X-Cdn-Uri"]; !ok {
		t.Error("CDN is not enabled")
	}
}

func TestCDNDisable(t *testing.T) {
	err := c.ContainerCDNDisable(context.Background(), CONTAINER) // files stick in CDN until TTL expires
	if err != nil {
		t.Error(err)
	}
}

// Teardown
func TestContainerDelete(t *testing.T) {
	err := c.ContainerDelete(context.Background(), CONTAINER)
	if err != nil {
		t.Fatal(err)
	}
}