Testing Strategies for https://github.com/thanos-io/thanos

Overview of testing frameworks and methodologies used in Thanos

Thanos, an open-source observability database, employs various testing strategies to ensure the reliability and functionality of its components. These testing strategies include unit testing, integration testing, and end-to-end testing.

Unit Testing

Unit tests are used to test individual components or functions in isolation. Thanos uses the testing package in Go for unit testing. For instance, unit tests for the net.SplitHostPort function can be found in the test/net_test.go file.

Integration Testing

Integration tests are used to test the interaction between different components or services. Thanos uses a combination of unit tests and ad-hoc testing for integration testing. For example, test_storeapi.go contains unit tests for the StoreAPI.

End-to-End Testing

End-to-end tests are used to test the entire system from an external perspective. Thanos does not have extensive end-to-end testing coverage, but some examples can be found in the test/e2e directory.

Insights

Thanos uses table-driven tests with the t.Run function for better readability and maintainability. The following example demonstrates testing the net.SplitHostPort function using table-driven tests.

Avoid πŸ”₯
β€œ`go host, port, err := net.SplitHostPort(β€œ1.2.3.4:1234”) testutil.Ok(t, err) testutil.Equals(t, β€œ1.2.3.4”, host) testutil.Equals(t, β€œ1234”, port)

host, port, err := net.SplitHostPort(β€œ1.2.3.4:something”) testutil.Ok(t, err) testutil.Equals(t, β€œ1.2.3.4”, host) testutil.Equals(t, β€œsomething”, port)

host, port, err := net.SplitHostPort(β€œ:1234”) testutil.Ok(t, err) testutil.Equals(t, β€œβ€, host) testutil.Equals(t, β€œ1234”, port)

host, port, err := net.SplitHostPort(β€œyolo”) testutil.NotOk(t, err)

</td></tr>
          <tr><th>Better πŸ€“</th></tr>
          <tr><td>
          ```go
          for _, tcase := range []struct{
          name string
          
          input     string
          expectedHost string
          expectedPort string
          expectedErr error
          }{
          {
          name: "host and port",
          
          input:     "1.2.3.4:1234",
          expectedHost: "1.2.3.4",
          expectedPort: "1234",
          },
          {
          name: "host and named port",
          
          input:     "1.2.3.4:something",
          expectedHost: "1.2.3.4",
          expectedPort: "something",
          },
          {
          name: "just port",
          
          input:     ":1234",
          expectedHost: "",
          expectedPort: "1234",
          },
          {
          name: "not valid hostport",
          
          input:     "yolo",
          expectedErr: errors.New("<exact error>"),
          },
          }{
          t.Run(tcase.name, func(t *testing.T) {
          host, port, err := net.SplitHostPort(tcase.input)
          if tcase.expectedErr != nil {
          testutil.NotOk(t, err)
          testutil.Equals(t, tcase.expectedErr, err)
          return
          }
          testutil.Ok(t, err)
          testutil.Equals(t, tcase.expectedHost, host)
          testutil.Equals(t, tcase.expectedPort, port)
          })
          }
          

Thanos also includes tests for various functions and components, such as the _over_time functions and the absent() function. These tests can be found in the test directory.