github.com/caos/orbos@v1.5.14-0.20221103111702-e6cd0cea7ad4/internal/operator/orbiter/kinds/providers/gce/computesservice_test.go (about)

     1  package gce_test
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"os"
     7  	"strings"
     8  	"testing"
     9  
    10  	secret2 "github.com/caos/orbos/pkg/secret"
    11  
    12  	"github.com/caos/orbos/internal/operator/orbiter/kinds/clusters/core/infra"
    13  	"github.com/caos/orbos/internal/operator/orbiter/kinds/providers/gce"
    14  	"github.com/caos/orbos/internal/ssh"
    15  	"github.com/caos/orbos/mntr"
    16  )
    17  
    18  func TestComputeService(t *testing.T) {
    19  
    20  	pool := &gce.Pool{
    21  		OSImage:     "projects/centos-cloud/global/images/centos-7-v20200429",
    22  		MinCPUCores: 2,
    23  		MinMemoryGB: 4,
    24  		StorageGB:   20,
    25  	}
    26  
    27  	private, public := ssh.Generate()
    28  
    29  	jsonKey := os.Getenv("ORBOS_GCE_JSON_KEY")
    30  	t.Log(jsonKey)
    31  	if jsonKey == "" {
    32  		t.Fatal("Environment variable ORBOS_GCE_JSON_KEY is empty")
    33  	}
    34  
    35  	svc := gce.NewMachinesService(
    36  		mntr.Monitor{OnInfo: mntr.LogMessage},
    37  		&gce.Spec{
    38  			Verbose: false,
    39  			JSONKey: &secret2.Secret{Value: jsonKey},
    40  
    41  			Region: "europe-west1",
    42  			Zone:   "europe-west1-b",
    43  			SSHKey: &gce.SSHKey{
    44  				Private: &secret2.Secret{Value: private},
    45  				Public:  &secret2.Secret{Value: public},
    46  			},
    47  			Pools: map[string]*gce.Pool{
    48  				"apool":       pool,
    49  				"anotherpool": pool,
    50  				"aThirdPool":  pool,
    51  			},
    52  		},
    53  		"gce",
    54  		"orbiter-elio",
    55  	)
    56  
    57  	machine, err := svc.Create("apool")
    58  	if err != nil {
    59  		t.Fatal(err)
    60  	}
    61  
    62  	buf := &bytes.Buffer{}
    63  	if err := machine.ReadFile("/home/orbiter/.ssh/authorized_keys", buf); err != nil {
    64  		t.Fatal(err)
    65  	}
    66  	t.Log(buf.String())
    67  
    68  	if err := machine.WriteFile("/var/lib/orbiter/hier", bytes.NewReader([]byte("da")), 600); err != nil {
    69  		t.Fatal(err)
    70  	}
    71  
    72  	buf.Reset()
    73  	if err := machine.ReadFile("/var/lib/orbiter/hier", buf); err != nil {
    74  		t.Fatal(err)
    75  	}
    76  	t.Log(buf.String())
    77  
    78  	stdout, err := machine.Execute(nil, "sudo whoami")
    79  	if err != nil {
    80  		t.Fatal(err)
    81  	}
    82  	t.Log(string(stdout))
    83  
    84  	if _, err := svc.Create("apool"); err != nil {
    85  		t.Fatal(err)
    86  	}
    87  
    88  	if _, err := svc.ListPools(); err != nil {
    89  		t.Fatal(err)
    90  	}
    91  
    92  	if _, err := svc.Create("anotherpool"); err != nil {
    93  		t.Fatal(err)
    94  	}
    95  
    96  	if _, err := svc.ListPools(); err != nil {
    97  		t.Fatal(err)
    98  	}
    99  
   100  	aPool, err := svc.List("apool")
   101  	if err != nil {
   102  		t.Fatal(err)
   103  	}
   104  
   105  	t.Log("apool:", sprint(aPool))
   106  
   107  	anotherPool, err := svc.List("anotherpool")
   108  	if err != nil {
   109  		t.Fatal(err)
   110  	}
   111  	t.Log("anotherpool:", sprint(anotherPool))
   112  	if err != nil {
   113  		t.Fatal(err)
   114  	}
   115  
   116  	if err := aPool[0].Remove(); err != nil {
   117  		t.Fatal(err)
   118  	}
   119  
   120  	aPool, err = svc.List("apool")
   121  	if err != nil {
   122  		t.Fatal(err)
   123  	}
   124  	t.Log("apool:", sprint(aPool))
   125  
   126  	if err := aPool[0].Remove(); err != nil {
   127  		t.Fatal(err)
   128  	}
   129  
   130  	aPool, err = svc.List("apool")
   131  	if err != nil {
   132  		t.Fatal(err)
   133  	}
   134  	t.Log("apool:", sprint(aPool))
   135  
   136  	if err := anotherPool[0].Remove(); err != nil {
   137  		t.Fatal(err)
   138  	}
   139  
   140  	aPool, err = svc.List("anotherpool")
   141  	if err != nil {
   142  		t.Fatal(err)
   143  	}
   144  	t.Log("anotherpool:", sprint(aPool))
   145  }
   146  
   147  func sprint(machines infra.Machines) string {
   148  	info := make([]string, len(machines))
   149  	for idx, machine := range machines {
   150  		info[idx] = fmt.Sprintf("%s (%s)", machine.ID(), machine.IP())
   151  	}
   152  	return strings.Join(info, ", ")
   153  }