Testing CSI Clients

If you are writing a CSI client, like a CO or a side car container, then you can use some of the following methods to test your application.

  • csi-test unit test mock driver: The csi-test repo provides an automatically generated Golang mock code to be used for unit tests.
  • mock-driver: This driver can be used as an external service to test your gRPC calls.
  • hostPath driver: This driver can be used on a single node to tests for mounting and unmounting of storage.

CSI-Test Unit Test Mock Driver

The csi-test unit test mock driver enables Golang clients to test all aspects of their code. This is done by using the mock driver generated using GoMock, which let's the caller verify parameters and test for returned values. Here is a small example:

    // Setup mock
    m := gomock.NewController(&mock_utils.SafeGoroutineTester{})
    defer m.Finish()
    driver := mock_driver.NewMockIdentityServer(m)

    // Setup input
    in := &csi.GetPluginInfoRequest{
        Version: &csi.Version{
            Major: 0,
            Minor: 1,
            Patch: 0,
        },
    }

    // Setup mock outout
    out := &csi.GetPluginInfoResponse{
        Name:          "mock",
        VendorVersion: "0.1.1",
        Manifest: map[string]string{
            "hello": "world",
        },
    }

    // Setup expectation
    // !IMPORTANT!: Must set context expected value to gomock.Any() to match any value
    driver.EXPECT().GetPluginInfo(gomock.Any(), in).Return(out, nil).Times(1)

    // Create a new RPC
    server := mock_driver.NewMockCSIDriver(&mock_driver.MockCSIDriverServers{
        Identity: driver,
    })
    conn, err := server.Nexus()
    if err != nil {
        t.Errorf("Error: %s", err.Error())
    }
    defer server.Close()

    // Make call
    c := csi.NewIdentityClient(conn)
    r, err := c.GetPluginInfo(context.Background(), in)
    if err != nil {
        t.Errorf("Error: %s", err.Error())
    }

    name := r.GetName()
    if name != "mock" {
        t.Errorf("Unknown name: %s\n", name)
    }

More Information

For more examples and information see:

HostPath Driver

The hostPath driver is probably the simplest CSI driver to use for testing on a single node. This is the driver that is for CSI e2e tests in Kubernetes. See the Example page for deployment and usage instructions.