github.com/freiheit-com/kuberpult@v1.24.2-0.20240328135542-315d5630abe6/tests/integration-tests/main_test.go (about)

     1  /*This file is part of kuberpult.
     2  
     3  Kuberpult is free software: you can redistribute it and/or modify
     4  it under the terms of the Expat(MIT) License as published by
     5  the Free Software Foundation.
     6  
     7  Kuberpult is distributed in the hope that it will be useful,
     8  but WITHOUT ANY WARRANTY; without even the implied warranty of
     9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    10  MIT License for more details.
    11  
    12  You should have received a copy of the MIT License
    13  along with kuberpult. If not, see <https://directory.fsf.org/wiki/License:Expat>.
    14  
    15  Copyright 2023 freiheit.com*/
    16  package integration_tests
    17  
    18  import (
    19  	"errors"
    20  	"fmt"
    21  	"os"
    22  	"strconv"
    23  	"testing"
    24  )
    25  
    26  // The appSuffix is a unique string consisting of only characters that are valid app names.
    27  // This is used to make tests repeatable.
    28  var appSuffix string
    29  
    30  func base26(i uint64) string {
    31  	result := ""
    32  	if i == 0 {
    33  		return "a"
    34  	}
    35  	for i > 0 {
    36  		rem := i % 26
    37  		i = i / 26
    38  		result = string(rune('a'+rem)) + result
    39  	}
    40  	return result
    41  }
    42  
    43  // The app suffix is calculate by storing a counter in a file called .run-number.
    44  func calculateAppSuffix() error {
    45  	content, err := os.ReadFile(".run-number")
    46  	if err != nil {
    47  		if errors.Is(err, os.ErrNotExist) {
    48  			content = []byte("0")
    49  		} else {
    50  			return err
    51  		}
    52  	}
    53  	nr, err := strconv.ParseUint(string(content), 10, 64)
    54  	if err != nil {
    55  		return err
    56  	}
    57  	nextRun := nr + 1
    58  	os.WriteFile(".run-number", []byte(fmt.Sprintf("%d", nextRun)), 0644)
    59  	appSuffix = base26(nr)
    60  	return nil
    61  }
    62  
    63  func TestMain(m *testing.M) {
    64  	err := calculateAppSuffix()
    65  	if err != nil {
    66  		fmt.Printf("%s", err)
    67  		os.Exit(2)
    68  	}
    69  	os.Exit(m.Run())
    70  }