Statistics
| Branch: | Revision:

grapes / src / Tests / cloud_test.c @ 98b047dd

History | View | Annotate | Download (5.16 KB)

1
/*
2
 *  Copyright (c) 2009 Andrea Zito
3
 *
4
 *  This is free software; see gpl-3.0.txt
5
 *
6
 *  This is a small test program for the cloud interface
7
 *  To try the simple test: run it with
8
 *    ./cloud_test -c "provider=<cloud_provider>,<provider_opts>" [-g key | -d key=value | -p key=value | -n variant | -e ip:port]
9
 *
10
 *    -g  GET key from cloud
11
 *    -d  GET key from cloud with default
12
 *    -p  PUT key=value on cloud
13
 *    -n  print the cloud node for the specified variant
14
 *    -e  check if ip:port references the cloud
15
 *    -c  set the configuration of the cloud provider
16
 *
17
 *    For example, run
18
 *      ./cloud_test -c "provider=delegate,delegate_lib=libfilecloud.so" -p hello=world
19
 *      ./cloud_test -c "provider=delegate,delegate_lib=libfilecloud.so" -g hello
20
 *
21
 *    to test the delegate cloud provider with delegate implementation provided by libfilecloud.so.
22
 */
23

    
24

    
25
#include <stdlib.h>
26
#include <stdio.h>
27
#include <string.h>
28
#include <getopt.h>
29
#include <time.h>
30

    
31
#include "cloud_helper.h"
32

    
33
#define GET 0
34
#define PUT 1
35
#define GET_CLOUD_NODE 2
36
#define EQ_CLOUD_NODE 3
37
#define GETDEF 4
38

    
39
static const char *config;
40
static int operation;
41
static int variant;
42
static char *key;
43
static char *value;
44

    
45
static const uint8_t *HEADER = (const uint8_t *) "<-header->";
46

    
47
static void cmdline_parse(int argc, char *argv[])
48
{
49
  int o;
50
  char *temp;
51

    
52
  while ((o = getopt(argc, argv, "c:g:d:p:n:e:")) != -1) {
53
    switch(o) {
54
    case 'c':
55
      config = strdup(optarg);
56
      break;
57
    case 'p':
58
      temp = strdup(optarg);
59
      operation = PUT;
60

    
61
      key = strsep(&optarg, "=");
62
      value = optarg;
63

    
64
      if (!value || !key){
65
        printf("Expected key=value for option -p");
66
        free(temp);
67
        exit(-1);
68
      }
69
      break;
70
    case 'g':
71
        key =  strdup(optarg);
72
        break;
73
    case 'd':
74
      temp = strdup(optarg);
75
      operation = GETDEF;
76

    
77
      key = strsep(&optarg, "=");
78
      value = optarg;
79

    
80
      if (!value || !key){
81
        printf("Expected key=value for option -d");
82
        free(temp);
83
        exit(-1);
84
      }
85
      break;
86
    case 'n':
87
      operation = GET_CLOUD_NODE;
88
      variant = atoi(optarg);
89
      break;
90
    case 'e':
91
      operation = EQ_CLOUD_NODE;
92
      temp = strdup(optarg);
93

    
94
      key = strsep(&optarg, ":");
95
      value = optarg;
96

    
97

    
98
      if (!value || !key){
99
        printf("Expected ip:port for option -e");
100
        free(temp);
101
        exit(-1);
102
      }
103
      break;
104
    default:
105
        fprintf(stderr, "Error: unknown option %c\n", o);
106

    
107
        exit(-1);
108
    }
109
  }
110
}
111

    
112
static struct cloud_helper_context *init(const char *config)
113
{
114
  struct nodeID *myID;
115
  struct cloud_helper_context *cloud_ctx;
116

    
117
  myID = net_helper_init("127.0.0.1", 1234, "");
118
  if (myID == NULL) {
119
    fprintf(stderr, "Error creating my socket\n");
120

    
121
    return NULL;
122
  }
123
  cloud_ctx = cloud_helper_init(myID, config);
124
  if (cloud_ctx == NULL) {
125
    fprintf(stderr, "Error initiating cloud_helper\n");
126
    return NULL;
127
  }
128

    
129
  return cloud_ctx;
130
}
131

    
132

    
133

    
134
int main(int argc, char *argv[])
135
{
136
  struct cloud_helper_context *cloud;
137
  char buffer[100];
138
  char addr[256];
139
  int err;
140
  struct nodeID *t;
141
  struct timeval tout = {10, 0};
142

    
143
  cmdline_parse(argc, argv);
144

    
145
  cloud = init(config);
146

    
147
  if (!cloud) return -1;
148

    
149
  switch(operation) {
150
  case PUT:
151
    printf("Putting on cloud value \"%s\" for key \"%s\"\n", value, key);
152
    err = put_on_cloud(cloud, key, value, strlen(value), 0);
153
    if (err) {
154
      printf("Error performing the operation");
155
      return 1;
156
    }
157
    break;
158
  case GETDEF:
159
  case GET:
160
    printf("Getting from cloud value for key \"%s\": ", key);
161
    memcpy(buffer, HEADER, strlen(HEADER));
162

    
163
    if (operation == GETDEF) {
164
      printf("(Using default value: \"%s\")", value);
165
      err = get_from_cloud_default(cloud, key, buffer, strlen(HEADER), 0,
166
                                   value, strlen(value), 0);
167
    } else {
168
      err = get_from_cloud(cloud, key, buffer, strlen(HEADER), 0);
169
    }
170

    
171
    if (err) {
172
      printf("Error performing the operation");
173
      return 1;
174
    }
175

    
176
    err = wait4cloud(cloud, &tout);
177
    if (err > 0) {
178
      err = recv_from_cloud(cloud, buffer, sizeof(buffer)-1);
179
      if (err < 0) {
180
        printf("Erorr receiving cloud response\n");
181
        return 1;
182
      } else {
183
        time_t timestamp;
184
        int i;
185
        buffer[err] = '\0';
186
        printf("len=%d\n", err);
187
        for (i=0; i<err; i++)
188
          printf("%x(%c) ", buffer[i], buffer[i]);
189
        printf("\n");
190
        timestamp = timestamp_cloud(cloud);
191
        printf("Timestamp: %s\n", ctime(&timestamp));
192
      }
193
    } else if (err == 0){
194
      printf("No response from cloud\n");
195
      return 1;
196
    } else {
197
      memset(buffer, 0, sizeof(buffer));
198
      err = recv_from_cloud(cloud, buffer, sizeof(buffer)-1);
199
      buffer[sizeof(buffer) - 1] = '\0';
200
      printf("No value for the specified key. Received: %s\n", buffer);
201
      return 1;
202
    }
203
    break;
204
  case GET_CLOUD_NODE:
205
    node_addr(get_cloud_node(cloud, variant), addr, 256);
206
    printf("Cloud node: %s\n", addr);
207
    break;
208
  case EQ_CLOUD_NODE:
209
    t = create_node(key, atoi(value));
210
    node_addr(get_cloud_node(cloud, variant), addr, 256);
211
    printf("Node %s references cloud? %d\n", addr,
212
           is_cloud_node(cloud, t));
213
    break;
214
  }
215

    
216

    
217
  return 0;
218
}