1// run
2
3// Copyright 2022 The Go Authors. All rights reserved.
4// Use of this source code is governed by a BSD-style
5// license that can be found in the LICENSE file.
6
7package main
8
9import (
10	"fmt"
11	"path/filepath"
12	"testing"
13)
14
15var t *testing.T
16
17type TypeMeta struct {
18	Kind       string
19	APIVersion string
20}
21
22type ObjectMeta struct {
23	Name         string `json:"name,omitempty" protobuf:"bytes,1,opt,name=name"`
24	GenerateName string `json:"generateName,omitempty" protobuf:"bytes,2,opt,name=generateName"`
25	Namespace    string `json:"namespace,omitempty" protobuf:"bytes,3,opt,name=namespace"`
26	SelfLink     string `json:"selfLink,omitempty" protobuf:"bytes,4,opt,name=selfLink"`
27}
28
29type ConfigSpec struct {
30	Disks        []DiskSpec
31	StorageClass string
32}
33
34type DiskSpec struct {
35	Name         string
36	Size         string
37	StorageClass string
38	Annotations  map[string]string
39	VolumeName   string
40}
41
42// Config is the Schema for the configs API.
43type Config struct {
44	TypeMeta
45	ObjectMeta
46
47	Spec ConfigSpec
48}
49
50func findDiskSize(diskSpec *DiskSpec, configSpec *ConfigSpec) string {
51	t.Log(fmt.Sprintf("Hello World"))
52	return diskSpec.Size
53}
54
55func findStorageClassName(diskSpec *DiskSpec, configSpec *ConfigSpec) *string {
56	if diskSpec.StorageClass != "" {
57		return &diskSpec.StorageClass
58	}
59
60	if configSpec != nil {
61		for _, d := range configSpec.Disks {
62			if d.Name == diskSpec.Name {
63				if d.StorageClass != "" {
64					return &d.StorageClass
65				}
66				break
67			}
68		}
69
70		if configSpec.StorageClass != "" {
71			return &configSpec.StorageClass
72		}
73	}
74	return nil
75}
76
77func Bar(config *Config) *ConfigSpec {
78	var configSpec *ConfigSpec
79	if config != nil {
80		configSpec = &config.Spec
81	}
82	return configSpec
83}
84
85func Foo(diskSpec DiskSpec, config *Config) {
86	cs := Bar(config)
87	_ = findDiskSize(&diskSpec, cs)
88	cs = Bar(config)
89	_ = findStorageClassName(&diskSpec, cs)
90
91}
92
93func TestPanic(tt *testing.T) {
94	t = tt
95	myarray := []string{filepath.Join("..", "config", "crd", "bases")}
96
97	for i := 0; i < 1000; i++ {
98		Foo(DiskSpec{
99			Name: "DataDisk",
100			Size: "1Gi",
101		}, nil)
102	}
103
104	t.Log(myarray)
105}
106
107// Hack to run tests in a playground
108func matchString(a, b string) (bool, error) {
109	return a == b, nil
110}
111func main() {
112	testSuite := []testing.InternalTest{
113		{
114			Name: "TestPanic",
115			F:    TestPanic,
116		},
117	}
118	testing.Main(matchString, testSuite, nil, nil)
119}
120