...

Source file src/golang.conradwood.net/tests/utils/test-utils.go

Documentation: golang.conradwood.net/tests/utils

     1  package main
     2  
     3  import (
     4  	"context"
     5  	"flag"
     6  	"fmt"
     7  	"os"
     8  	"sync"
     9  	"time"
    10  
    11  	echoservice "golang.conradwood.net/apis/getestservice"
    12  	"golang.conradwood.net/go-easyops/authremote"
    13  	"golang.conradwood.net/go-easyops/objectstore"
    14  	"golang.conradwood.net/go-easyops/utils"
    15  )
    16  
    17  var (
    18  	fs = utils.FlagStrings("test_action_flag", "this is just a test", map[string]string{"on": "turn something on", "off": "turn something off", "auto": "turn something on/off depending on something else"})
    19  )
    20  
    21  func main() {
    22  	flag.Parse()
    23  	TestMulti()
    24  	TestIPs()
    25  	TestTime()
    26  	td, err := utils.TerminalSize()
    27  	utils.Bail("failed to get terminal dimensions", err)
    28  	fmt.Printf("Terminal is %dx%d size\n", td.Columns(), td.Rows())
    29  	fmt.Printf("Checking random string generator...\n")
    30  	var wg sync.WaitGroup
    31  	for j := 0; j < 1000; j++ {
    32  		wg.Add(1)
    33  		go func() {
    34  			for i := 1; i < 1000; i++ {
    35  				s := utils.RandomString(i)
    36  				if len(s) != i {
    37  					panic(fmt.Sprintf("invalid length %d != %d", i, len(s)))
    38  				}
    39  			}
    40  			wg.Done()
    41  		}()
    42  	}
    43  	wg.Wait()
    44  	err = utils.DirWalk("/etc/systemd", func(root, rel string) error {
    45  		fmt.Printf("rel: %s\n", rel)
    46  		return nil
    47  	})
    48  	utils.Bail("dirwalk failed", err)
    49  
    50  	dir := "/lib"
    51  	fmt.Printf("md5sum across %s, twice...\n", dir)
    52  	hash1, err := utils.DirHash(dir)
    53  	utils.Bail("failed to create dirhash #1", err)
    54  	fmt.Printf("Hash #1: %s\n", hash1)
    55  	hash2, err := utils.DirHash(dir)
    56  	utils.Bail("failed to create dirhash #2", err)
    57  	fmt.Printf("Hash #2: %s\n", hash2)
    58  	if hash1 != hash2 {
    59  		fmt.Printf("Hashes inconsistent\n")
    60  		os.Exit(10)
    61  	}
    62  
    63  	//
    64  
    65  	check_store()
    66  	t := time.Now()
    67  	fmt.Printf("Time now: %s\n", utils.TimeString(t))
    68  	t, err = utils.LocalTime(context.Background())
    69  	utils.Bail("failed to get timezone", err)
    70  	fmt.Printf("Time local: %s\n", utils.TimeString(t))
    71  	print(14)
    72  	print(65)
    73  	print(125)
    74  	print(120)
    75  	print(0)
    76  	print(60*60 + 3 + 24)
    77  	print(60*60*2 + 60*5 + 40)
    78  	secs := uint32(0)
    79  	fmt.Printf("'not set' as Age: %s\n", utils.TimestampAgeString(secs))
    80  	s := fmt.Sprintf("I am a test hexdump buffer with some text and stuff\n")
    81  	fmt.Println(utils.HexdumpWithLen(16, "hex ", []byte(s)))
    82  }
    83  
    84  func print(age int) {
    85  	secs := uint32(time.Now().Unix()) - uint32(age)
    86  	fmt.Printf("%d seconds as Age: %s\n", age, utils.TimestampAgeString(secs))
    87  }
    88  func check_store() {
    89  	ctx := authremote.Context()
    90  	pr := &echoservice.PingRequest{
    91  		SequenceNumber: uint32(utils.RandomInt(500)),
    92  		Payload:        utils.RandomString(100),
    93  	}
    94  	utils.Bail("failed to store proto", objectstore.StoreProto(ctx, "goeasyops-test", pr))
    95  	npr := &echoservice.PingRequest{}
    96  	utils.Bail("failed to retrieve proto", objectstore.RetrieveProto(ctx, "goeasyops-test", npr))
    97  	if pr.SequenceNumber != npr.SequenceNumber {
    98  		fmt.Printf("ERROR: Store/Retrieve proto: Sequence number mismatch\n (%d != %d)", pr.SequenceNumber, npr.SequenceNumber)
    99  		os.Exit(10)
   100  	}
   101  	if pr.Payload != npr.Payload {
   102  		fmt.Printf("ERROR: Store/Retrieve proto: payload mismatch\n (%s != %s)", pr.Payload, npr.Payload)
   103  		os.Exit(10)
   104  	}
   105  
   106  }
   107  

View as plain text