DEV Community

Mohammad Waseem
Mohammad Waseem

Posted on

Streamlining Test Account Management with Go and Open Source Tools

Managing Test Accounts Efficiently: A Go-Based Approach

In the realm of software testing, especially in environments with complex user interactions, managing test accounts becomes a critical bottleneck. Traditional methods—manual creation, environment-specific scripts, or tracking through spreadsheets—are error-prone and resource-intensive. As a Lead QA Engineer, I sought an automation-driven solution leveraging Go and open source tools to streamline this process.

The Challenge

Test accounts are essential for end-to-end testing across different environments. However, creating, updating, and tearing down these accounts can lead to inconsistencies, security concerns, and increased setup time. The main goals were:

  • Automate test account provisioning and cleanup
  • Maintain idempotency and consistency
  • Integrate seamlessly with existing CI pipelines
  • Minimize external dependencies

Solution Overview

Using Go as the core language for its simplicity, concurrency support, and mature ecosystem, I designed a modular system that interacts with the application's API, a local database for state management, and open source tools for configuration and orchestration.

Core Components

  • Go HTTP Client: To interact with the application's account management API.
  • BoltDB: A lightweight embedded key-value store for tracking test accounts.
  • Cobra & Viper: For CLI commands and configuration management.
  • Go Concurrency: To handle multiple account operations simultaneously.

Implementation Details

1. Structuring the Application

I defined a TestAccountManager struct encapsulating dependencies:

type TestAccountManager struct {
    apiEndpoint string
    db          *bolt.DB
    client      *http.Client
}
Enter fullscreen mode Exit fullscreen mode

2. Account Provisioning

The process involves creating accounts via API calls and storing their details locally:

func (m *TestAccountManager) CreateAccount() (string, error) {
    reqBody := map[string]string{"action": "create"}
    jsonData, _ := json.Marshal(reqBody)

    req, _ := http.NewRequest("POST", m.apiEndpoint, bytes.NewBuffer(jsonData))
    req.Header.Set("Content-Type", "application/json")

    resp, err := m.client.Do(req)
    if err != nil { return "", err }
    defer resp.Body.Close()

    var res map[string]string
    json.NewDecoder(resp.Body).Decode(&res)

    accountID := res["account_id"]
    m.db.Update(func(tx *bolt.Tx) error {
        bucket, _ := tx.CreateBucketIfNotExists([]byte("accounts"))
        return bucket.Put([]byte(accountID), []byte("active"))
    })
    return accountID, nil
}
Enter fullscreen mode Exit fullscreen mode

3. Cleanup and Reuse

Test accounts can be recycled to avoid API spamming:

func (m *TestAccountManager) DeleteAccount(accountID string) error {
    reqBody := map[string]string{"action": "delete", "account_id": accountID}
    jsonData, _ := json.Marshal(reqBody)
    req, _ := http.NewRequest("POST", m.apiEndpoint, bytes.NewBuffer(jsonData))
    req.Header.Set("Content-Type", "application/json")

    resp, err := m.client.Do(req)
    if err != nil { return err }
    defer resp.Body.Close()

    m.db.Update(func(tx *bolt.Tx) error {
        bucket := tx.Bucket([]byte("accounts"))
        return bucket.Delete([]byte(accountID))
    })
    return nil
}
Enter fullscreen mode Exit fullscreen mode

4. CLI Tooling

Using Cobra, I built commands to interact with the system for creating, listing, and deleting test accounts, enabling automation within CI/CD pipelines.

var rootCmd = &cobra.Command{
    Use:   "testaccount",
    Short: "Manage test accounts",
}

var createCmd = &cobra.Command{
    Use:   "create",
    Short: "Create a new test account",
    Run: func(cmd *cobra.Command, args []string) {
        manager := NewTestAccountManager()
        accountID, err := manager.CreateAccount()
        if err != nil {
            fmt.Println("Error creating account:", err)
            return
        }
        fmt.Println("Created account ID:", accountID)
    },
}

rootCmd.AddCommand(createCmd)

// Similar commands for list and delete
Enter fullscreen mode Exit fullscreen mode

Benefits and Results

By automating test account management with Go and open source tools, we achieved:

  • Significant reduction in manual overhead
  • Consistent test environments
  • Faster test runs and CI/CD integration
  • Secure handling of credentials through API calls without exposing sensitive information

Conclusion

Leveraging Go's performance, concurrency, and simplicity along with tools like BoltDB and Cobra, I built a robust system that solves a common QA pain point. This approach not only streamlines test account management but also scales easily and integrates seamlessly into complex testing frameworks.

This pattern can be adapted for various resource provisioning challenges in testing or staging environments, demonstrating how open source tools and thoughtful design can empower QA teams to do more with less.


🛠️ QA Tip

To test this safely without using real user data, I use TempoMail USA.

Top comments (0)