Path: blob/main/components/ws-daemon/pkg/quota/xfs_test.go
2499 views
// Copyright (c) 2021 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package quota56import (7"fmt"8"sort"9"strings"10"testing"1112"github.com/google/go-cmp/cmp"13)1415func TestGetUsedProjectIDs(t *testing.T) {16type Expectation struct {17ProjectIDs []int18Error string19}20tests := []struct {21Name string22Input string23InputErr error24Expectation Expectation25}{26{27Name: "no projects",28Input: "",29},30{31Name: "single project",32Input: "#0 4 0 0 00 [------]",33Expectation: Expectation{34ProjectIDs: []int{0},35},36},37{38Name: "multiple projects none used",39Input: "#0 0 0 0 00 [------]\n#100 0 5M 5M 00 [------]\n#200 0 10M 10M 00 [------]",40},41{42Name: "multiple projects in use",43Input: "#0 0 0 0 00 [------]\n#100 4 5M 5M 00 [------]\n#200 1 10M 10M 00 [------]",44Expectation: Expectation{45ProjectIDs: []int{100, 200},46},47},48{49Name: "exec failure",50InputErr: fmt.Errorf("exec failed"),51Expectation: Expectation{52Error: "exec failed",53},54},55}5657for _, test := range tests {58t.Run(test.Name, func(t *testing.T) {59xfs := &XFS{60exec: func(dir, command string) (output string, err error) {61return test.Input, test.InputErr62},63}6465var (66act Expectation67err error68)69act.ProjectIDs, err = xfs.getUsedProjectIDs()70if err != nil {71act.Error = err.Error()72}7374if diff := cmp.Diff(test.Expectation, act); diff != "" {75t.Errorf("unexpected getUsedProjectIDs (-want +got):\n%s", diff)76}77})78}79}8081func TestSetQuota(t *testing.T) {82type Expectation struct {83ProjectID int84ProjectIDs []int85Execs []string86Error string87}88tests := []struct {89Name string90Size Size91IsHard bool92ExecErr func(cmd string) error93ProjectIDs []int94Expectation Expectation95}{96{97Name: "happpy path",98Size: 100 * Kilobyte,99IsHard: true,100Expectation: Expectation{101ProjectID: 1000,102ProjectIDs: []int{1000},103Execs: []string{104"project -s -d 1 -p /foo 1000",105"limit -p bhard=102400 1000",106},107},108},109{110Name: "with soft limit",111Size: 100 * Kilobyte,112IsHard: false,113Expectation: Expectation{114ProjectID: 1000,115ProjectIDs: []int{1000},116Execs: []string{117"project -s -d 1 -p /foo 1000",118"limit -p bsoft=102400 1000",119},120},121},122{123Name: "with other prj",124Size: 100 * Kilobyte,125IsHard: true,126ProjectIDs: []int{1000},127Expectation: Expectation{128ProjectID: 1001,129ProjectIDs: []int{1000, 1001},130Execs: []string{131"project -s -d 1 -p /foo 1001",132"limit -p bhard=102400 1001",133},134},135},136{137Name: "prj creation failure",138Size: 100 * Kilobyte,139IsHard: true,140ExecErr: func(cmd string) error {141if strings.Contains(cmd, "project") {142return fmt.Errorf("failed to create project")143}144return nil145},146Expectation: Expectation{147ProjectID: 0,148Execs: []string{149"project -s -d 1 -p /foo 1000",150},151Error: "failed to create project",152},153},154}155156for _, test := range tests {157t.Run(test.Name, func(t *testing.T) {158var (159act Expectation160err error161)162xfs := &XFS{163exec: func(dir, command string) (output string, err error) {164act.Execs = append(act.Execs, command)165if test.ExecErr != nil {166return "", test.ExecErr(command)167}168169return "", nil170},171projectIDs: make(map[int]struct{}),172Dir: "/",173}174for _, prjid := range test.ProjectIDs {175xfs.projectIDs[prjid] = struct{}{}176}177178act.ProjectID, err = xfs.SetQuota("/foo", test.Size, test.IsHard)179if err != nil {180act.Error = err.Error()181}182for p := range xfs.projectIDs {183act.ProjectIDs = append(act.ProjectIDs, p)184}185sort.Ints(act.ProjectIDs)186187if diff := cmp.Diff(test.Expectation, act); diff != "" {188t.Errorf("unexpected SetQuota (-want +got):\n%s", diff)189}190})191}192}193194195