From 16df830f6cf22b926df6be539fade00fc8d27073 Mon Sep 17 00:00:00 2001
From: bryanl <bryanliles@gmail.com>
Date: Sat, 10 Mar 2018 10:46:04 -0500
Subject: [PATCH] don't fail fetching api spec when json is invalid

If server returned a malformed json response, GetAPISpec panics

Signed-off-by: bryanl <bryanliles@gmail.com>
---
 client/client.go             | 12 ++++-----
 client/client_test.go        | 47 ++++++++++++++++++++++++++++++++++++
 client/testdata/swagger.json |  5 ++++
 3 files changed, 58 insertions(+), 6 deletions(-)
 create mode 100644 client/client_test.go
 create mode 100644 client/testdata/swagger.json

diff --git a/client/client.go b/client/client.go
index 5561f70a..f2c93af0 100644
--- a/client/client.go
+++ b/client/client.go
@@ -36,6 +36,10 @@ import (
 	"k8s.io/client-go/tools/clientcmd"
 )
 
+const (
+	defaultVersion = "version:v1.7.0"
+)
+
 // Config is a wrapper around client-go's ClientConfig
 type Config struct {
 	Overrides    *clientcmd.ConfigOverrides
@@ -72,10 +76,6 @@ func InitClient(env string) (dynamic.ClientPool, discovery.DiscoveryInterface, s
 // If no swagger is found, or we are unable to authenticate to the server, we
 // will default to version:v1.7.0.
 func (c *Config) GetAPISpec(server string) string {
-	const (
-		defaultVersion = "version:v1.7.0"
-	)
-
 	type Info struct {
 		Version string `json:"version"`
 	}
@@ -111,8 +111,8 @@ func (c *Config) GetAPISpec(server string) string {
 	}
 
 	spec := Spec{}
-	jsonErr := json.Unmarshal(body, &spec)
-	if jsonErr != nil {
+	err = json.Unmarshal(body, &spec)
+	if err != nil {
 		log.Debugf("Failed to parse swagger at %s\n%s", url, err.Error())
 		return defaultVersion
 	}
diff --git a/client/client_test.go b/client/client_test.go
new file mode 100644
index 00000000..d474510f
--- /dev/null
+++ b/client/client_test.go
@@ -0,0 +1,47 @@
+package client
+
+import (
+	"fmt"
+	"io/ioutil"
+	"net/http"
+	"net/http/httptest"
+	"testing"
+
+	"github.com/stretchr/testify/require"
+)
+
+func TestConfig_GetAPISpec(t *testing.T) {
+	b, err := ioutil.ReadFile("testdata/swagger.json")
+	require.NoError(t, err)
+
+	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+		fmt.Fprintln(w, string(b))
+	}))
+	defer ts.Close()
+
+	cases := []struct {
+		name      string
+		serverURL string
+		expected  string
+	}{
+		{
+			name:      "invalid server URL",
+			serverURL: "http://+++",
+			expected:  defaultVersion,
+		},
+		{
+			name:      "with a server",
+			serverURL: ts.URL,
+			expected:  "version:v1.9.3",
+		},
+	}
+
+	for _, tc := range cases {
+		t.Run(tc.name, func(t *testing.T) {
+			c := Config{}
+			got := c.GetAPISpec(tc.serverURL)
+			require.Equal(t, tc.expected, got)
+		})
+	}
+
+}
diff --git a/client/testdata/swagger.json b/client/testdata/swagger.json
new file mode 100644
index 00000000..342e4dbd
--- /dev/null
+++ b/client/testdata/swagger.json
@@ -0,0 +1,5 @@
+{
+    "info": {
+        "version": "v1.9.3"
+    }
+}
\ No newline at end of file
-- 
GitLab