diff --git a/Makefile b/Makefile index 1336358a..35b58bd7 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ PLATFORM ?= linux/amd64 REGISTRY_NAME ?= index.docker.io IMAGE_NAME ?= linode/linode-blockstorage-csi-driver -REV := $(shell git branch --show-current 2> /dev/null || echo "dev") +REV := $(shell git describe --long --tags --dirty 2> /dev/null || echo "dev") IMAGE_VERSION ?= $(REV) IMAGE_TAG ?= $(REGISTRY_NAME)/$(IMAGE_NAME):$(IMAGE_VERSION) GOLANGCI_LINT_IMG := golangci/golangci-lint:v1.59-alpine diff --git a/internal/driver/driver_test.go b/internal/driver/driver_test.go index 33d1127d..4211805f 100644 --- a/internal/driver/driver_test.go +++ b/internal/driver/driver_test.go @@ -3,17 +3,13 @@ package driver import ( "context" "fmt" - "net/http/httptest" "os" "testing" "github.com/linode/linode-blockstorage-csi-driver/mocks" - drivertest "github.com/linode/linode-blockstorage-csi-driver/pkg/driver-test" linodeclient "github.com/linode/linode-blockstorage-csi-driver/pkg/linode-client" "k8s.io/mount-utils" - "github.com/linode/linodego" - "go.uber.org/mock/gomock" ) @@ -31,25 +27,6 @@ func TestDriverSuite(t *testing.T) { bsPrefix := "test-" - // mock Linode Server, not working yet ... - fake := &drivertest.FakeAPI{ - T: t, - Volumes: map[string]linodego.Volume{}, - Instance: &linodego.Instance{ - Label: "linode123", - Region: "us-east", - Image: "linode/debian9", - Type: "g6-standard-2", - Group: "Linode-Group", - ID: 123, - Status: "running", - Hypervisor: "kvm", - }, - } - - ts := httptest.NewServer(fake) - defer ts.Close() - mockCtrl := gomock.NewController(t) defer mockCtrl.Finish() @@ -61,7 +38,7 @@ func TestDriverSuite(t *testing.T) { fileSystem := mocks.NewMockFileSystem(mockCtrl) encrypt := NewLuksEncryption(mounter.Exec, fileSystem) - fakeCloudProvider, err := linodeclient.NewLinodeClient("dummy", fmt.Sprintf("LinodeCSI/%s", vendorVersion), ts.URL) + fakeCloudProvider, err := linodeclient.NewLinodeClient("dummy", fmt.Sprintf("LinodeCSI/%s", vendorVersion), "") if err != nil { t.Fatalf("Failed to setup Linode client: %s", err) } @@ -69,8 +46,8 @@ func TestDriverSuite(t *testing.T) { // TODO fake metadata md := Metadata{ ID: 123, - Label: fake.Instance.Label, - Region: fake.Instance.Region, + Label: "linode123", + Region: "us-east", Memory: 4 << 30, // 4GiB } linodeDriver := GetLinodeDriver(context.Background()) diff --git a/pkg/driver-test/fake-api.go b/pkg/driver-test/fake-api.go deleted file mode 100644 index d4e3c3a0..00000000 --- a/pkg/driver-test/fake-api.go +++ /dev/null @@ -1,198 +0,0 @@ -package drivertest - -import ( - "encoding/json" - "fmt" - "math/rand" - "net/http" - "path/filepath" - "strconv" - "strings" - "testing" - "time" - - "github.com/linode/linodego" -) - -// fakeAPI implements a fake, cached Linode API -type FakeAPI struct { - T *testing.T - Volumes map[string]linodego.Volume - Instance *linodego.Instance -} - -func (f *FakeAPI) ServeHTTP(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "application/json") - switch r.Method { - case "GET": - switch { - case strings.HasPrefix(r.URL.Path, "/v4/volumes/"): - // single volume get - id := filepath.Base(r.URL.Path) - vol, ok := f.Volumes[id] - if ok { - rr, _ := json.Marshal(vol) - _, _ = w.Write(rr) - } else { - w.WriteHeader(404) - resp := linodego.APIError{ - Errors: []linodego.APIErrorReason{ - {Reason: "Not Found"}, - }, - } - rr, _ := json.Marshal(resp) - _, _ = w.Write(rr) - } - return - case strings.HasPrefix(r.URL.Path, "/v4/volumes"): - res := 0 - data := []linodego.Volume{} - - var filters map[string]string - hf := r.Header.Get("X-Filter") - if hf != "" { - _ = json.Unmarshal([]byte(hf), &filters) - } - - for _, vol := range f.Volumes { - - if filters["label"] != "" && filters["label"] != vol.Label { - continue - } - data = append(data, vol) - } - resp := linodego.VolumesPagedResponse{ - PageOptions: &linodego.PageOptions{ - Page: 1, - Pages: 1, - Results: res, - }, - Data: data, - } - rr, _ := json.Marshal(resp) - _, _ = w.Write(rr) - return - case strings.HasPrefix(r.URL.Path, "/v4/linode/instances/"): - id := filepath.Base(r.URL.Path) - if id == strconv.Itoa(f.Instance.ID) { - rr, _ := json.Marshal(&f.Instance) - _, _ = w.Write(rr) - return - } else { - w.WriteHeader(404) - resp := linodego.APIError{ - Errors: []linodego.APIErrorReason{ - {Reason: "Not Found"}, - }, - } - rr, _ := json.Marshal(resp) - _, _ = w.Write(rr) - } - case strings.HasPrefix(r.URL.Path, "/v4/linode/instances"): - res := 1 - data := []linodego.Instance{} - - data = append(data, *f.Instance) - resp := linodego.InstancesPagedResponse{ - PageOptions: &linodego.PageOptions{ - Page: 1, - Pages: 1, - Results: res, - }, - Data: data, - } - rr, _ := json.Marshal(resp) - _, _ = w.Write(rr) - return - - } - - case "POST": - tp := filepath.Base(r.URL.Path) - var vol linodego.Volume - var found bool - if tp == "attach" { - v := new(linodego.VolumeAttachOptions) - if err := json.NewDecoder(r.Body).Decode(v); err != nil { - f.T.Fatal(err) - } - parts := strings.Split(r.URL.Path, "/") - if len(parts) != 4 { - f.T.Fatal("url not good") - } - vol, found = f.Volumes[parts[2]] - if !found { - w.WriteHeader(404) - resp := linodego.APIError{ - Errors: []linodego.APIErrorReason{ - {Reason: "Not Found"}, - }, - } - rr, _ := json.Marshal(resp) - _, _ = w.Write(rr) - return - } - if vol.LinodeID != nil { - f.T.Fatal("volume already attached") - return - } - vol.LinodeID = &v.LinodeID - f.Volumes[parts[2]] = vol - - } else if tp == "detach" { - parts := strings.Split(r.URL.Path, "/") - if len(parts) != 4 { - f.T.Fatal("url not good") - } - vol, found = f.Volumes[parts[2]] - if !found { - w.WriteHeader(404) - resp := linodego.APIError{ - Errors: []linodego.APIErrorReason{ - {Reason: "Not Found"}, - }, - } - rr, _ := json.Marshal(resp) - _, _ = w.Write(rr) - return - } - vol.LinodeID = nil - f.Volumes[parts[2]] = vol - return - } else { - v := new(linodego.VolumeCreateOptions) - err := json.NewDecoder(r.Body).Decode(v) - if err != nil { - f.T.Fatal(err) - } - - id := rand.Intn(99999) - name := v.Label - path := fmt.Sprintf("/dev/disk/by-id/scsi-0Linode_Volume_%v", name) - now := time.Now() - vol = linodego.Volume{ - ID: id, - Region: v.Region, - Label: name, - Size: v.Size, - FilesystemPath: path, - Status: linodego.VolumeActive, - Tags: v.Tags, - Created: &now, - Updated: &now, - } - - f.Volumes[strconv.Itoa(id)] = vol - - } - - resp, err := json.Marshal(vol) - if err != nil { - f.T.Fatal(err) - } - _, _ = w.Write(resp) - case "DELETE": - id := filepath.Base(r.URL.Path) - delete(f.Volumes, id) - } -}