Ad meliora

Nov 27, 2023

Adventures in Golang (Tests)

OUTLINE

PROBLEM STATEMENT

So I had finished the first version of my code implementing a npm registry server in Go. This version has no tests. I have been testing it manually. I wanted to add filesystem locking and deploy the code through CI. So I needed to add automated tests(unit and integration) as I wanted complement my manual testing.

Here is a snippet of what I wanted to write a unit test for:

package handler

import (
    "fmt"
    "net/http"
    "net/url"

    "github.com/gorilla/mux"
    "github.com/sirupsen/logrus"

    "gosimplenpm/internal/config"
    "gosimplenpm/internal/storage"
)

func GetPackage(lg *logrus.Logger, cfg config.Config) http.HandlerFunc {
    return func(w http.ResponseWriter, r *http.Request) {
        escapedName := mux.Vars(r)["name"]
        packageName, _ := url.PathUnescape(escapedName)
        lg.WithFields(logrus.Fields{
            "function": "get-package",
        }).Debugf("Package name => %s\n", packageName)

        fileToServe, found, err := storage.GetIndexJsonFromStore(packageName, cfg.RepoDir, lg)
        if err != nil {
            http.Error(w, err.Error(), http.StatusInternalServerError)
            return
        }

        if !found {
            ret := fmt.Sprintf("Package not found: %s", packageName)
            http.Error(w, ret, http.StatusNotFound)
            return
        }

        // serve file
        http.ServeFile(w, r, fileToServe)
    }
}

So I wanted to test this function without also testing the storage.GetIndexJsonFromStore function. The storage.GetIndexJsonFromStore function makes calls to the filesystem. Well, coming from Javascript, I would use a mocking framework like Sinon.JS 1 to mock out the storage.GetIndexJsonFromStore function without modifying the GetPackage handler.

FIRST APPROACH

So using my limited knowledge of Go, I wrote an interface Storage and defined the method GetIndexJsonFromStore like below:

package storage

import (
    "gosimplenpm/internal/serviceidos"

    "github.com/sirupsen/logrus"
)

type Storage interface {
    GetIndexJsonFromStore(string, string, *logrus.Logger) (string, bool, error)
    // Other methods omitted for brevity
}

Then I can define a struct MockFS that would implement the method GetIndexJsonFromStore. This is because structs that implement all the methods of an interface are objects of that interface.

type MockFs struct {
    packageJson               string
    retrieved                 bool
    err                       error
    called                    bool
}

func (m *MockFs) SetError(err error) {
    m.err = err
}

func (m *MockFs) SetRetrieved(retrieved bool) {
    m.retrieved = retrieved
}

func (m *MockFs) SetFileToServe(fileToServe string) {
    m.packageJson = fileToServe
}

func (m *MockFs) GetIndexJsonFromStore(packageName string, registryPath string, lg *logrus.Logger) (string, bool, error) {
    m.called = true
    return m.packageJson, m.retrieved, m.err
}

I would then modify the previously defined GetIndexJsonFromStore method to allow for me to inject the MockFs dependency.

package handler

import (
    "fmt"
    "net/http"
    "net/url"

    "github.com/gorilla/mux"
    "github.com/sirupsen/logrus"

    "gosimplenpm/internal/config"
    "gosimplenpm/internal/storage"
)

func GetPackage(lg *logrus.Logger, cfg config.Config, stg storage.Storage) http.HandlerFunc {
    return func(w http.ResponseWriter, r *http.Request) {
        escapedName := mux.Vars(r)["name"]
        packageName, _ := url.PathUnescape(escapedName)
        lg.WithFields(logrus.Fields{
            "function": "get-package",
        }).Debugf("Package name => %s\n", packageName)

        fileToServe, found, err := stg.GetIndexJsonFromStore(packageName, cfg.RepoDir, lg)
        if err != nil {
            http.Error(w, err.Error(), http.StatusInternalServerError)
            return
        }

        if !found {
            ret := fmt.Sprintf("Package not found: %s", packageName)
            http.Error(w, ret, http.StatusNotFound)
            return
        }

        // serve file
        http.ServeFile(w, r, fileToServe)
    }
}

A sample test would be something like this

package handler

import (
    "bytes"
    "fmt"
    "gosimplenpm/internal/config"
    "gosimplenpm/internal/storage"
    "io"
    "net/http"
    "net/http/httptest"
    "os"
    "testing"

    "github.com/gorilla/mux"
    "github.com/sirupsen/logrus"
    "github.com/stretchr/testify/assert"
)

func TestGet(t *testing.T) {
    t.Run("return `Not Found` error if package is not found", func(t *testing.T) {
        req := httptest.NewRequest(http.MethodGet, "/{name}", nil)
        wrt := httptest.NewRecorder()

        log := &logrus.Logger{
            Out: os.Stdout,
            Formatter: &logrus.TextFormatter{
                FullTimestamp:   true,
                TimestampFormat: "2009-01-02 15:15:15",
            },
        }

        cfg := config.Config{
            RepoDir: "",
        }

        mfs := &storage.MockFs{}
        mfs.SetRetrieved(false)


        //Hack to try to fake gorilla/mux vars
        vars := map[string]string{
            "name": "test-package",
        }

        req = mux.SetURLVars(req, vars)

        GetPackage(log, cfg, mfs)(wrt, req)

        rs := wrt.Result()

        assert.Equal(t, rs.StatusCode, http.StatusNotFound)

        defer rs.Body.Close()
        body, err := io.ReadAll(rs.Body)
        if err != nil {
            t.Fatal(err)
        }
        bytes.TrimSpace(body)

        assert.Equal(t, string(body), "Package not found: test-package\n")
    })
}

This approach fails if I wanted to test another function stg.GetOtherJsonFromNet of the interface like below:

package handler

import (
    "fmt"
    "net/http"
    "net/url"

    "github.com/gorilla/mux"
    "github.com/sirupsen/logrus"

    "gosimplenpm/internal/config"
    "gosimplenpm/internal/storage"
)

func GetPackage(lg *logrus.Logger, cfg config.Config, stg storage.Storage) http.HandlerFunc {
    return func(w http.ResponseWriter, r *http.Request) {
        escapedName := mux.Vars(r)["name"]
        packageName, _ := url.PathUnescape(escapedName)
        lg.WithFields(logrus.Fields{
            "function": "get-package",
        }).Debugf("Package name => %s\n", packageName)

        fileToServe, found, err := stg.GetIndexJsonFromStore(packageName, cfg.RepoDir, lg)
        if err != nil {
            http.Error(w, err.Error(), http.StatusInternalServerError)
            return
        }
        if !found {
            fileToServe, err = stg.GetOtherJsonFromNet(packageName, lg)
            if err != nil {
                http.Error(w, err.Error(), http.StatusInternalServerError)
                return
            }
        }
        //...code omitted for brevity
    }
}

SECOND APPROACH

To handle this new scenario, that is, to test the other function GetOtherJsonFromNet as well as GetIndexJsonFromStore, I would modify the struct and its implementations to something like this:

type MockFs struct {
    GetIndexJsonFromStoreFunc func(packageName string, registryPath string, lg *logrus.Logger) (string, bool, error)
    GetOtherJsonFromNetFunc func(packageName string, lg *logrus.Logger) (string, error)
}

func (m *MockFs) GetIndexJsonFromStore(packageName string, registryPath string, lg *logrus.Logger) (string, bool, error) {
    return m.GetIndexJsonFromStoreFunc(packageName, registryPath, lg)
}

func (m *MockFs) GetOtherJsonFromNet(packageName string, lg *logrus.Logger) (string, error) {
    return m.GetOtherJsonFromNetFunc(packageName, lg)
}

Basically since I cannot reassign a function to an already defined struct (as I could in Javascript with objects), I would instead define function variables as part of the MockFS struct. That way, I can redefine those function variables for each test I want to run. An example of a test that uses this is shown below:

package handler

import (
    "bytes"
    "fmt"
    "gosimplenpm/internal/config"
    "gosimplenpm/internal/storage"
    "io"
    "net/http"
    "net/http/httptest"
    "os"
    "testing"

    "github.com/gorilla/mux"
    "github.com/sirupsen/logrus"
    "github.com/stretchr/testify/assert"
)

func TestGet(t *testing.T) {
    t.Run("return `Internal Server` error if package cannot be retrieved either from disk or from the internet", func(t *testing.T) {
        req := httptest.NewRequest(http.MethodGet, "/{name}", nil)
        wrt := httptest.NewRecorder()

        log := &logrus.Logger{
            Out: os.Stdout,
            Formatter: &logrus.TextFormatter{
                FullTimestamp:   true,
                TimestampFormat: "2009-01-02 15:15:15",
            },
        }

        cfg := config.Config{
            RepoDir: "",
        }

        mfs := &storage.MockFs{}
        mfs.GetIndexJsonFromStore = func (packageName string, registryPath string, lg *logrus.Logger) (string, bool, error) {
            return "", false, fmt.Errorf("Failure to retrieve disk")
        }
        mfs.GetOtherJsonFromNet = func (packageName string, lg *logrus.Logger) (string, error) {
            return "", false, fmt.Errorf("Failure to retrieve from internet")
        }

        //Hack to try to fake gorilla/mux vars
        vars := map[string]string{
            "name": "test-package",
        }

        req = mux.SetURLVars(req, vars)

        GetPackage(log, cfg, mfs)(wrt, req)

        rs := wrt.Result()

        assert.Equal(t, rs.StatusCode, http.StatusInternalServerErro)

        defer rs.Body.Close()
        body, err := io.ReadAll(rs.Body)
        if err != nil {
            t.Fatal(err)
        }
        bytes.TrimSpace(body)

        assert.Equal(t, string(body), "Failure to retrieve from internet\n")
    })
}

This approach avoids the usage of struct-level variables, making it easier to easily fine-tune your tests.

Now, I had already moved all my tests to this approach before I found out about GoMock2. In the future, I may move to GoMock2 instead.