11/*
2- Copyright 2023 API Testing Authors.
2+ Copyright 2023-2024 API Testing Authors.
33
44Licensed under the Apache License, Version 2.0 (the "License");
55you may not use this file except in compliance with the License.
@@ -16,8 +16,13 @@ limitations under the License.
1616package extension
1717
1818import (
19+ "context"
20+ "os"
21+ "path/filepath"
1922 "testing"
23+ "time"
2024
25+ "github.com/spf13/cobra"
2126 "github.com/spf13/pflag"
2227 "github.com/stretchr/testify/assert"
2328)
@@ -54,3 +59,78 @@ func TestExtension(t *testing.T) {
5459 assert .NotNil (t , flags .Lookup ("port" ))
5560 assert .NotNil (t , flags .Lookup ("socket" ))
5661}
62+
63+ func TestCreateRunner (t * testing.T ) {
64+
65+ t .Run ("invalid port" , func (t * testing.T ) {
66+ extMgr := NewExtension ("git" , "store" , 75530 )
67+ extMgr .Port = 75530
68+ assert .NotNil (t , extMgr )
69+ assert .Error (t , CreateRunner (extMgr , nil , nil ))
70+ assert .Error (t , CreateMonitor (extMgr , nil , nil ))
71+ assert .Error (t , CreateExtensionRunner (extMgr , nil , nil ))
72+ })
73+
74+ t .Run ("random port" , func (t * testing.T ) {
75+ extMgr := NewExtension ("git" , "store" , - 1 )
76+
77+ ctx , cancel := context .WithTimeout (context .Background (), time .Second )
78+ defer cancel ()
79+ command := & cobra.Command {}
80+ command .SetContext (ctx )
81+ assert .Error (t , CreateRunner (extMgr , command , nil ))
82+ })
83+
84+ t .Run ("random port, CreateMonitor" , func (t * testing.T ) {
85+ extMgr := NewExtension ("git" , "store" , - 1 )
86+
87+ ctx , cancel := context .WithTimeout (context .Background (), time .Second )
88+ defer cancel ()
89+ command := & cobra.Command {}
90+ command .SetContext (ctx )
91+ assert .Error (t , CreateMonitor (extMgr , command , nil ))
92+ })
93+
94+ t .Run ("random port, CreateExtensionRunner" , func (t * testing.T ) {
95+ extMgr := NewExtension ("git" , "store" , - 1 )
96+
97+ ctx , cancel := context .WithTimeout (context .Background (), time .Second )
98+ defer cancel ()
99+ command := & cobra.Command {}
100+ command .SetContext (ctx )
101+ assert .Error (t , CreateExtensionRunner (extMgr , command , nil ))
102+ })
103+
104+ t .Run ("socket" , func (t * testing.T ) {
105+ extMgr := NewExtension ("git" , "store" , - 1 )
106+ extMgr .Socket = filepath .Join (os .TempDir (), time .Microsecond .String ())
107+
108+ ctx , cancel := context .WithTimeout (context .Background (), time .Second )
109+ defer cancel ()
110+ command := & cobra.Command {}
111+ command .SetContext (ctx )
112+ assert .Error (t , CreateRunner (extMgr , command , nil ))
113+ })
114+
115+ t .Run ("socket, CreateMonitor" , func (t * testing.T ) {
116+ extMgr := NewExtension ("git" , "store" , - 1 )
117+ extMgr .Socket = filepath .Join (os .TempDir (), time .Microsecond .String ())
118+
119+ ctx , cancel := context .WithTimeout (context .Background (), time .Second )
120+ defer cancel ()
121+ command := & cobra.Command {}
122+ command .SetContext (ctx )
123+ assert .Error (t , CreateMonitor (extMgr , command , nil ))
124+ })
125+
126+ t .Run ("socket, CreateExtensionRunner" , func (t * testing.T ) {
127+ extMgr := NewExtension ("git" , "store" , - 1 )
128+ extMgr .Socket = filepath .Join (os .TempDir (), time .Microsecond .String ())
129+
130+ ctx , cancel := context .WithTimeout (context .Background (), time .Second )
131+ defer cancel ()
132+ command := & cobra.Command {}
133+ command .SetContext (ctx )
134+ assert .Error (t , CreateExtensionRunner (extMgr , command , nil ))
135+ })
136+ }
0 commit comments