Browse Source

Merge pull request #46175 from vvoland/pkg-plugins-gotest

pkg/plugins: Rewrite with assert.Check
Sebastiaan van Stijn 1 year ago
parent
commit
89b542b421
1 changed files with 20 additions and 38 deletions
  1. 20 38
      pkg/plugins/plugin_test.go

+ 20 - 38
pkg/plugins/plugin_test.go

@@ -16,6 +16,7 @@ import (
 	"github.com/docker/go-connections/tlsconfig"
 	"github.com/pkg/errors"
 	"gotest.tools/v3/assert"
+	is "gotest.tools/v3/assert/cmp"
 )
 
 const (
@@ -71,30 +72,23 @@ func TestGet(t *testing.T) {
 
 	t.Run("success", func(t *testing.T) {
 		plugin, err := Get(fruitPlugin, fruitImplements)
-		if err != nil {
-			t.Fatal(err)
-		}
-		if p.Name() != plugin.Name() {
-			t.Errorf("no matching plugin with name %s found", plugin.Name())
-		}
-		if plugin.Client() != nil {
-			t.Error("expected nil Client but found one")
-		}
-		if !plugin.IsV1() {
-			t.Error("Expected true for V1 plugin")
-		}
+		assert.NilError(t, err)
+
+		assert.Check(t, is.Equal(p.Name(), plugin.Name()))
+		assert.Check(t, is.Nil(plugin.Client()))
+		assert.Check(t, plugin.IsV1())
 	})
 
 	// check negative case where plugin fruit doesn't implement banana
 	t.Run("not implemented", func(t *testing.T) {
 		_, err := Get("fruit", "banana")
-		assert.Assert(t, errors.Is(err, ErrNotImplements))
+		assert.Check(t, is.ErrorIs(err, ErrNotImplements))
 	})
 
 	// check negative case where plugin vegetable doesn't exist
 	t.Run("not exists", func(t *testing.T) {
 		_, err := Get(testNonExistingPlugin, "no-such-implementation")
-		assert.Assert(t, errors.Is(err, ErrNotFound))
+		assert.Check(t, is.ErrorIs(err, ErrNotFound))
 	})
 }
 
@@ -105,14 +99,11 @@ func TestPluginWithNoManifest(t *testing.T) {
 
 	m := Manifest{[]string{fruitImplements}}
 	var buf bytes.Buffer
-	if err := json.NewEncoder(&buf).Encode(m); err != nil {
-		t.Fatal(err)
-	}
+	err := json.NewEncoder(&buf).Encode(m)
+	assert.NilError(t, err)
 
 	mux.HandleFunc("/Plugin.Activate", func(w http.ResponseWriter, r *http.Request) {
-		if r.Method != http.MethodPost {
-			t.Fatalf("Expected POST, got %s\n", r.Method)
-		}
+		assert.Assert(t, is.Equal(r.Method, http.MethodPost))
 
 		header := w.Header()
 		header.Set("Content-Type", transport.VersionMimetype)
@@ -131,12 +122,8 @@ func TestPluginWithNoManifest(t *testing.T) {
 	storage.Unlock()
 
 	plugin, err := Get(fruitPlugin, fruitImplements)
-	if err != nil {
-		t.Fatal(err)
-	}
-	if p.Name() != plugin.Name() {
-		t.Fatalf("No matching plugin with name %s found", plugin.Name())
-	}
+	assert.NilError(t, err)
+	assert.Check(t, is.Equal(p.name, plugin.Name()))
 }
 
 func TestGetAll(t *testing.T) {
@@ -154,24 +141,19 @@ func TestGetAll(t *testing.T) {
 	"Addr": "https://example.com/docker/plugin"
 }`
 
-	if err := os.WriteFile(p, []byte(spec), 0o644); err != nil {
-		t.Fatal(err)
-	}
+	err := os.WriteFile(p, []byte(spec), 0o644)
+	assert.NilError(t, err)
 
 	plugin, err := r.Plugin("example")
-	if err != nil {
-		t.Fatal(err)
-	}
+	assert.NilError(t, err)
+
 	plugin.Manifest = &Manifest{Implements: []string{"apple"}}
 	storage.Lock()
 	storage.plugins["example"] = plugin
 	storage.Unlock()
 
 	fetchedPlugins, err := r.GetAll("apple")
-	if err != nil {
-		t.Fatal(err)
-	}
-	if fetchedPlugins[0].Name() != plugin.Name() {
-		t.Fatalf("Expected to get plugin with name %s", plugin.Name())
-	}
+	assert.NilError(t, err)
+	assert.Check(t, is.Len(fetchedPlugins, 1))
+	assert.Check(t, is.Equal(fetchedPlugins[0].Name(), plugin.Name()))
 }